os.walk()

os.walk()返回三个参数:os.walk(dirpath,dirnames,filenames)

for dirpath,dirnames,filenames in os.walk():

返回dirnames,filenames的类型为列表list[]

返回的dirpath为filenames的上级路径,如果要获得全路径和文件名,以便open的话,可以用在for filename in filenames的循环下用os.path.join(dirpath,filename)进行拼接

常见的用法:

for root,dirs,files in os.walk(sourcedir):
  for filename in files:
    keyname = os.path.join(root.replace(sourcedir+'\\',''),filename)

posted @ 2017-01-04 15:35  Norwegian-Wood  阅读(516)  评论(0编辑  收藏  举报