This question already has answers here:
Find all files in a directory with extension .txt in Python
(25 answers)
Closed 4 years ago.
My objective is to print out all available files that end in '.txt' inside a folder, I'm unsure how to do so.
Thank you for reading.
you can use
filename.endswith('.txt')
simply just open the folder by
fileList = os.listdir("folder_path") #open that folder
for filename in fileList:
if(filename.endswith('.txt')): # check its extension
print(filename)
Related
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.
This question already has answers here:
Rename multiple files in Python [duplicate]
(7 answers)
Closed 1 year ago.
I have over 2000 images in a folder that needs to be renamed. Currently, it's a default name as w=0&h=-_BACl339n_c4PTZDlVgaHWg9s1k_Vyz8PbhNhhXkQk=0 and I need it to name it as fear_1 and all the other images in this format. Is there any way to achieve this?
Use os.listdir to get all the filenames and os.rename to rename them
import os
path = '//path//to//folder'
files = os.listdir(path)
for index, file in enumerate(files):
os.rename(os.path.join(path, file), os.path.join(path, ''.join(['fear_',str(index+1), '.jpg'])))
This question already has answers here:
Directory-tree listing in Python
(21 answers)
Closed 9 years ago.
How to read all files and also the filenames?
I am using MAC so is there any there a different way to give path on MAC in Python?
Maybe something like this? Or os.listdir() is simpler if you don't need recursion.
Even on Windows, Python abstracts away the differences between operating systems if you use it well.
#!/usr/local/cpython-3.3/bin/python
import os
def main():
for root, dirs, files in os.walk('/home/dstromberg/src/outside-questions'):
for directory in dirs:
print('directory', os.path.join(root, directory))
for file_ in files:
print('file', os.path.join(root, file_))
main()
See http://docs.python.org/3/library/os.html for more info.
This question already has answers here:
Directory-tree listing in Python
(21 answers)
Closed 9 years ago.
I want to retrieve the filenames of all the files with .xml extension present in various subfolders in a single directory,
code:
import os
xmlFiles = []
for directoryPath in os.walk(filePath):
fileName = directoryPath[2]
if fileName[:3] = 'xml':# or fileName.endswith('xml'):
xmlFiles.append(fileName)
Use os.walk, and str.endswith.
This question already has answers here:
Monitoring contents of files/directories? [duplicate]
(5 answers)
Closed 8 years ago.
How can I iterate all files and subdirs in a Dir,
and can detect new file when put there?
Thank you for your help!
Try os.walk. More specifically, try:
top="."
import os
for root, dirs, files in os.walk(top):
for name in files:
# do something with each file as 'name' (a)
pass
for name in dirs:
# do something with each subdir as 'name' (b)
pass
# do something with root (dir path so far)
# break at any point if necessary
To answer the question in your comment, at point (b) in the code, you can handle any subdirectory logic (also you can check to test that you have the right subdirectory to do certain custom logic on), via another function or directly/inline.