开发者社区 问答 正文

python怎么迭代文件

python怎么迭代文件

展开
收起
云计算小粉 2018-05-10 20:11:03 1575 分享 版权
2 条回答
写回答
取消 提交回答
  • image.png

    2019-11-27 17:03:03
    赞同 展开评论
  • import os

    for folderName, subfolders, filenames in os.walk('C:\delicious'):

    print('The current folder is ' + folderName)
    
    for subfolder in subfolders:
        print('SUBFOLDER OF ' + folderName + ': ' + subfolder)
    for filename in filenames:
        print('FILE INSIDE ' + folderName + ': '+ filename)
    

    也可以用下面的
    for fname in os.listdir('.'):

    if os.path.isdir(fname):
       pass  # do your stuff here for directory
    else:
       pass  # do your stuff here for regular file
    

    作者:黄哥
    链接:https://www.zhihu.com/question/45396739/answer/98821802
    来源:知乎
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    2019-07-17 22:24:58
    赞同 展开评论
问答分类:
问答标签:
问答地址: