python 获取指定文件夹下所有文件名称并写入列表的实例
如下所示:
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()
在做深度学习的时候,会有大量的数据,为了方便,可以直接用命令拷贝到一个文件夹下,代码如下:
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)
相关推荐
FlySky 2020-11-02
逍遥友 2020-10-26
taiyangshenniao 2020-10-05
flycony 2020-09-23
jacktangj 2020-09-18
YENCSDN 2020-09-15
lsjweiyi 2020-09-14
digwtx 2020-09-14
拾毅者 2020-09-14
zlxcsdn 2020-09-13
weiiron 2020-08-17
amazingbo 2020-08-16
郗瑞强 2020-08-16
lispython 2020-08-16
fengling 2020-08-15
xiesheng 2020-08-02
葫芦小金刚 2020-07-28
StevenSun空间 2020-07-26