Retrieving the filenames from subdirectories [duplicate] - python

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.

Related

How to rename image name in a folder using python? [duplicate]

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'])))

While iterating through files, how to append each filename to a list? [duplicate]

This question already has answers here:
how to split out the file name from path by different characters in python?
(2 answers)
Extract file name from path, no matter what the os/path format
(22 answers)
Split filenames with python
(6 answers)
How do I get the filename without the extension from a path in Python?
(31 answers)
Closed 2 years ago.
I am iterating through a folder of files, to extract some text from an xml, and wish to keep track of which file each text match came from.
I am looking to put the filenames into the filename_master list. I think I may be over-complicating by using a regex (each filename has 14 digits.xml) but this isn't coming to me.
path = '/Users/Downloads/PDF/XML/'
read_files = glob.glob(os.path.join(path, '*.xml'))
filename_master=[]
text_master=[]
for file in read_files:
parse = ET.parse(file)
root = parse.getroot()
all_nodes = list(root.iter())
ls=[ele.text for ele in all_nodes if ele.findall('[#mark="1"]')]
my_exp = re.compile(r'.*(\d{14})\.xml')
name = my_exp.match(file).group(1)
filename_master.append(name)
text_master.append(ls)
If you are sure that every file has 14 digits, you may
name = file[-18:-4]
filename_master.append(name)
or if you are in linux environment (where "/" is path seperator):
name = file.split('/')[-1][:-4]
filename_master.append(name)
or better:
name = os.path.basename(file)[:-4]
filename_master.append(name)
but using regex is fine IMHO.

Printing out all titles ending in '.txt' ( Python ) [duplicate]

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)

Python: Read only file name, instead of path [duplicate]

This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Closed 5 years ago.
How to get ONLY filename instead of full path?
For example:
path = /folder/file.txt
and i need to get:
filename = file.txt
How to do that?
You should use the os module:
import os
filename = os.path.basename(path)
For other path manipulations look here (for python 2.7) or here (for python 3)

Read all files in a folder and also the filenames in python? [duplicate]

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.

Categories