Home > Article > Backend Development > Python example of getting the names of all files in a specified folder and writing them into a list
Below I will share with you an example of using python to obtain the names of all files in a specified folder and write them into a list. It has a good reference value and I hope it will be helpful to everyone. Come and take a look together
As shown below:
import os import os.path rootdir = "./pic_data" file_object = open('train_list.txt','w') for parent,dirnames,filenames in os.walk(rootdir): for filename in filenames: print filename file_object.write(filename+ '\n') file_object.close()
at When doing deep learning, there will be a large amount of data. For convenience, you can directly copy it to a folder with the command. The code is as follows:
import shutil import os import os.path rootdir = "./mjsynth/mnt/ramdisk/max/90kDICT32px" #rootdir = "./123" def Test2(rootDir): for lists in os.listdir(rootDir): #如果找到的是图片,则打印出来 if lists[-3:]=='jpg': print lists path = os.path.join(rootDir, lists) shutil.copy(path,"./500") continue #如果找到的是文件夹,则判断,如果名称小于2则递归 if int(lists)<501: path = os.path.join(rootDir, lists) if os.path.isdir(path): Test2(path) Test2(rootdir)
Related recommendations:
Instances of python getting file path, file name, suffix name
##
The above is the detailed content of Python example of getting the names of all files in a specified folder and writing them into a list. For more information, please follow other related articles on the PHP Chinese website!