How to find what files are in a folder [duplicate] - python

This question already has answers here:
Directory-tree listing in Python
(21 answers)
Closed 9 years ago.
I am writing a function that is recieving a folder path as an arguemnt. I want her to add into a dictionary what's inside the folder (like dir in CMD)
How can I do this ?
Thank you in advance,
Iliya

import os
print os.listdir('/tmp')
Similar Topics:
Directory listing in Python
Also, I use os.path and glob a lot while manipulating file system path.
You might want to check it out.

Related

Why glob is not getting files in subfolders [duplicate]

This question already has answers here:
How to use glob() to find files recursively?
(28 answers)
Closed 17 days ago.
I have following directory structure
$ find
.
./file1.html
./soquest_glob.py
./dir1
./dir1/file2.html
./dir2
./dir2/file3.html
(I have added blank lines above to clarify files in different folders).
I am trying to find all html files (including those in subfolders) with following Python code using glob package:
$ cat soquest_glob.py
import glob
flist = glob.glob("*.html", recursive=True)
print(flist)
However, when I run this code, it finds only file in current folder, not in subfolders:
$ python3 soquest_glob.py
['file1.html']
Where is the problem and how can it be solved?
The recursive argument to glob.glob effects the behavior of **. You need to use a pattern like: glob.glob("**/*.html", recursive=True).

How to get the path of the file and not the current working directory [duplicate]

This question already has answers here:
How do I get the path and name of the file that is currently executing?
(26 answers)
Closed 3 years ago.
So I have a python file sample.py which is in let's say 'A/B/C' and my current working directory is let's say 'D/E'. Now in sample.py I am needed it's directory that is 'A/B/C' . Can anyone help me with this. I have tried "dir_path = os.path.dirname(os.path.realpath('__sample__'))" but it returns the current working directory.
Change the line:
dir_path = os.path.dirname(os.path.realpath('__sample__'))
to
dir_path = os.path.dirname(os.path.realpath(__file__))
This should work for you. Find more about __file__ here.

How do I delete files from the directory using python [duplicate]

This question already has answers here:
How to delete a file by extension in Python?
(7 answers)
Closed 4 years ago.
want to delete pdf files from the directory using python.
Having "pdffiles" name folder in that lost of pdf are there So I want to delete all files from it but don't want to delete folder, want to delete just file from folder. how can I do it.(may be using os.remove())
Try listdir+remove:
import os
for i in os.listdir('directory path'):
if i.endswith('.pdf'):
os.remove(i)

How to change file extension with Python? [duplicate]

This question already has answers here:
Extracting extension from filename in Python
(33 answers)
Closed 7 years ago.
I'm trying to make a program that will take a file, say my_test_file.log and make a new file called my_test_file.mdn. I'd like to be able to use this program by typing python renameprogram.py my_test_file.log into the command line. The original file will always end in .log.
from shutil import copyfile
from glob import glob
map(lambda x:copyfile(x,x[:-3]+"mdn"),glob("*.log"))
or perhaps more simply
...
import sys
copyfile(sys.argv[1],sys.argv[1][:-3]+"mdn")
You certainly can create a Python program that will accomplish this, but there are shell level commands that already do this.
For Linux/Unix:
mv my_test_file.log my_test_file.mdn
For Windows (see this link):
rename my_test_file.log my_test_file.mdn

Using Python, how do you remove files given a filespec such as "*.obj" on Windows? [duplicate]

This question already has answers here:
Get a filtered list of files in a directory
(14 answers)
Closed 8 years ago.
How do you remove files given a filespec such as "*.obj" on Windows? I'm using Windows 7 and 8.1 at the moment.
Evidently os.remove does not take filespecs ("filespec" being a crude regular-expression for including wildcards such as *.txt to mean all files that end with ".txt").
The python glob module provides wildcard file matching. So
import glob
import os
for f in glob.glob("*.obj"):
os.remove(f)

Categories