python中的walk()怎么用

pyhon中的walk()怎么用
2024-12-02 21:41:27
推荐回答(2个)
回答1:

#这是用来将子目录里的东西都列出来
import re
import os
typetofind="cue"
rr=re.compile("\.%s$"%typetofind,re.I)
rootdirs=[r"D:\TDDOWNLOAD\music",r"d:\tddownload\movie"]
ii=0
for rootdir in rootdirs:
wholefiles=os.walk(rootdir)
for p,d,f in wholefiles:
for file in f:
if rr.search(file):
ii=ii+1
print("file%d:"%ii+p+"\\"+file)

print("I have:%d "%ii+typetofind)

回答2:

for root, dirs, files in os.walk('/home'):
for file in files:
print os.path.join(root, file)