国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python列出一個(gè)文件夾及其子目錄的所有文件

2019-11-25 16:40:48
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

python簡(jiǎn)介

Python是一種解釋型、面向?qū)ο蟆?dòng)態(tài)數(shù)據(jù)類型的高級(jí)程序設(shè)計(jì)語(yǔ)言。

Python由Guido van Rossum于1989年底發(fā)明,第一個(gè)公開發(fā)行版發(fā)行于1991年。

像Perl語(yǔ)言一樣, Python 源代碼同樣遵循 GPL(GNU General Public License)協(xié)議。

>>> import os>>> for i in os.walk("."):... print i[0],"/n##",i[1],"/n##",i[2]... . #當(dāng)前目錄## ['fa', 'out'] #當(dāng)前目錄中的子目錄 ## ['meta_rna.sh', 'nohup.out', 'log.cpu', 'blast_seq.py']./fa # 第一個(gè)子目錄## [] # 第一個(gè)子目錄中的目錄## ['assemblyar_new_2.faa']./out # 第二個(gè)子目錄## [] # 第二個(gè)子目錄中的目錄## ['assemblyar_new_2.faa.coord', 'assemblyar_new_2.faa.mask', 'assemblyar_new_2.faa.seq', 'result_1.xm', 'result.xml', 'blast_seq.py']

也可以用 os.path.walk, 先定義一個(gè)訪問(wèn)文件夾的函數(shù), VisitDir

>>> def VisitDir(arg, dirname, names):... for filespath in names:... print os.path.join(dirname, filespath)... >>> path=".">>> os.path.walk(path, VisitDir, ())./meta_rna.sh./fa./out./nohup.out./log.cpu./blast_seq.py./fa/assemblyar_new_2.faa./out/assemblyar_new_2.faa.coord./out/assemblyar_new_2.faa.mask./out/assemblyar_new_2.faa.seq./out/result_1.xm./out/result.xml./out/blast_seq.py>>> os.getcwd()'/home/served_pro/Find_nick'>>> abs_path= os.getcwd()>>> os.path.walk(abs_path, VisitDir, ())/home/served_pro/Find_nick/meta_rna.sh/home/served_pro/Find_nick/fa/home/served_pro/Find_nick/out/home/served_pro/Find_nick/nohup.out/home/served_pro/Find_nick/log.cpu/home/served_pro/Find_nick/blast_seq.py/home/served_pro/Find_nick/fa/assemblyar_new_2.faa/home/served_pro/Find_nick/out/assemblyar_new_2.faa.coord/home/served_pro/Find_nick/out/assemblyar_new_2.faa.mask/home/served_pro/Find_nick/out/assemblyar_new_2.faa.seq/home/served_pro/Find_nick/out/result_1.xm/home/served_pro/Find_nick/out/result.xml/home/served_pro/Find_nick/out/blast_seq.py

下面給大家介紹python列出文件夾下的所有文件

#方法1:使用os.listdirimport osfor filename in os.listdir(r'c://windows'):print filename#方法2:使用glob模塊,可以設(shè)置文件過(guò)濾import globfor filename in glob.glob(r'c://windows//*.exe'):print filename#方法3:通過(guò)os.path.walk遞歸遍歷,可以訪問(wèn)子文件夾import os.pathdef processDirectory ( args, dirname, filenames ):print 'Directory',dirnamefor filename in filenames:print ' File',filenameos.path.walk(r'c://windows', processDirectory, None )#方法4:非遞歸import osfor dirpath, dirnames, filenames in os.walk('c:////winnt'):print 'Directory', dirpathfor filename in filenames:print ' File', filename

另外,判斷文件與目錄是否存在:

import osos.path.isfile('test.txt') #如果不存在就返回Falseos.path.exists(directory) #如果目錄不存在就返回False

以上所述是小編給大家介紹的Python列出一個(gè)文件夾及其子目錄的所有文件,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 灌云县| 定南县| 翁牛特旗| 昌黎县| 平泉县| 缙云县| 兴安县| 台南县| 麻江县| 辛集市| 云林县| 海淀区| 泰顺县| 唐山市| 安泽县| 福州市| 同江市| 乌兰浩特市| 兰西县| 叶城县| 桑植县| 理塘县| 济南市| 本溪| 揭阳市| 沭阳县| 卓尼县| 东至县| 加查县| 依安县| 河南省| 雷山县| 唐山市| 开封市| 聂拉木县| 涞水县| 巴林右旗| 钟山县| 昌黎县| 西城区| 紫阳县|