This question already has answers here:
List only files in a directory?
(8 answers)
how to check if a file is a directory or regular file in python? [duplicate]
(4 answers)
Closed 2 months ago.
I have a directory and need to get all files in it, but not subdirectories.
I have found os.listdir(path) but that gets subdirectories as well.
My current temporary solution is to then filter the list to include only the things with '.' in the title (since files are expected to have extensions, .txt and such) but that is obviously not optimal.
We can create an empty list called my_files and iterate through the files in the directory. The for loop checks to see if the current iterated file is not a directory. If it is not a directory, it must be a file.
my_files = []
for i in os.listdir(path):
if not os.path.isdir(i):
my_files.append(i)
That being said, you can also check if it is a file instead of checking if it is not a directory, by using if os.path.isfile(i).
I find this approach is simpler than glob because you do not have to deal with any path joining.
Related
This question already has answers here:
Extracting specific files within directory - Windows
(2 answers)
Closed 3 years ago.
I am running a loop which needs to access circa 200 files in the directory.
In the folder - the format of the files range as follows:
Excel_YYYYMMDD.txt
Excel_YYYYMMDD_V2.txt
Excel_YYYYMMDD_orig.txt
I only need to extract the first one - that is YYYYMMDD.txt, and nothing else
I am using glob.glob to access the directory where I specified my path name as follows:
path = "Z:\T\Al8787\Box\EAST\OT\\ABB files/2019/*[0-9].txt"
However the code also extracts the .Excel_YYYYMMDD_orig.txt file too
Appreciate assistance on how to modify code to only extract desired files.
A simple solution would be to loop through the files returned by glob.glob(path). For example if
files = glob.glob("Z:\T\Al8787\Box\EAST\OT\\ABB files/2019/*[0-9].txt")
you could have
cleaned_files = [file for file in files if "orig" not in files]
This would remove every item in files that contains the substring orig
Maybe you should incorporate a split function into the code:
var=path.split('whatever letter separates them')
Then print out that variable.
This question already has answers here:
Rename multiple files inside multiple folders
(3 answers)
Closed 4 years ago.
i'm trying to erase all indexes (characters) except the last 4 ones and the files' extension in python. for example:
a2b-0001.tif to 0001.tif
a3tcd-0002.tif to 0002.tif
as54d-0003.tif to 0003.tif
Lets say that folders "a", "b" and "c" which contains those tifs files are located in D:\photos
there many of those files in many folders in D:\photos
that's where i got so far:
import os
os.chdir('C:/photos')
for dirpath, dirnames, filenames in os.walk('C:/photos'):
os.rename (filenames, filenames[-8:])
why that' not working?
So long as you have Python 3.4+, pathlib makes it extremely simple to do:
import pathlib
def rename_files(path):
## Iterate through children of the given path
for child in path.iterdir():
## If the child is a file
if child.is_file():
## .stem is the filename (minus the extension)
## .suffix is the extension
name,ext = child.stem, child.suffix
## Rename file by taking only the last 4 digits of the name
child.rename(name[-4:]+ext)
directory = pathlib.Path(r"C:\photos").resolve()
## Iterate through your initial folder
for child in directory.iterdir():
## If the child is a folder
if child.is_dir():
## Rename all files within that folder
rename_files(child)
Just note that because you're truncating file names, there may be collisions which may result in files being overwritten (i.e.- files named 12345.jpg and 22345.jpg will both be renamed to 2345.jpg, with the second overwriting the first).
This question already has answers here:
How to list only top level directories in Python?
(21 answers)
Closed 2 years ago.
How can I bring python to only output directories via os.listdir, while specifying which directory to list via raw_input?
What I have:
file_to_search = raw_input("which file to search?\n>")
dirlist=[]
for filename in os.listdir(file_to_search):
if os.path.isdir(filename) == True:
dirlist.append(filename)
print dirlist
Now this actually works if I input (via raw_input) the current working directory. However, if I put in anything else, the list returns empty. I tried to divide and conquer this problem but individually every code piece works as intended.
that's expected, since os.listdir only returns the names of the files/dirs, so objects are not found, unless you're running it in the current directory.
You have to join to scanned directory to compute the full path for it to work:
for filename in os.listdir(file_to_search):
if os.path.isdir(os.path.join(file_to_search,filename)):
dirlist.append(filename)
note the list comprehension version:
dirlist = [filename for filename in os.listdir(file_to_search) if os.path.isdir(os.path.join(file_to_search,filename))]
This question already has answers here:
os.walk() ValueError: need more than 1 value to unpack
(4 answers)
Closed 9 years ago.
Can I somehow save the output of os.walk() in variables ? I tried
basepath, directories, files = os.walk(path)
but it didn't work. I want to proceed the files of the directory and one specific subdirectory. is this somehow possible ? Thanks
os.walk() returns a generator that will successively return all the tree of files/directories from the initial path it started on. If you only want to process the files in a directory and one specific subdirectory you should use a mix of os.listdir() and a mixture of os.path.isfile() and os.path.isdir() to get what you want.
Something like this:
def files_and_subdirectories(root_path):
files = []
directories = []
for f in os.listdir(root_path):
if os.path.isfile(f):
files.append(f)
elif os.path.isdir(f):
directories.append(f)
return directories, files
And use it like so:
directories,files = files_and_subdirectories(path)
I want to proceed the files of the directory and one specific
subdirectory. is this somehow possible ?
If that's all you want then simply try
[e for e in os.listdir('.') if os.path.isfile(e)] + os.listdir(special_dir)
This question already has answers here:
How to copy a file along with directory structure/path using python? [duplicate]
(2 answers)
Closed 8 years ago.
I want to copy a certain file to a specified path. This specified path has many hierarchies of directories which do not exist in advance and need to get created during copying.
I tried shutil.copy* functions but they all seem to require that the directory at destination path is pre-created.
Is there any functions that sets up these directories as required and copies the file?
Example usage:
copy_file('resources/foo.bar', expanduser('~/a/long/long/path/foo.bar'))
You can use os.makedirs to recursively create the arborescence you need, then use shutil.copy.
target_dir = os.path.expanduser('~/a/long/long/path')
os.makedirs(target_dir)
shutil.copy('resources/foo.bar', os.path.join(target_dir, 'foo_bar'))
That way, you decompose the problem in manageable tasks (creation then copy), which lets you handle the case where the creation of the directories crashes (following the 'explicit is better than implicit').