码迷,mamicode.com
首页 > 编程语言 > 详细

Python.遍历目标路径下所有

时间:2020-03-11 01:23:23      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:dia   director   imp   code   路径   只读   dial   pass   遍历   

import stat
from tkinter.filedialog import *


def traversal_dir(dir_name):
    rets = []
    try:
        if len(dir_name):
            for r, ds, fs in os.walk(dir_name):
                for i1 in fs:
                    rets.append(str(os.path.join(r, i1)).replace('\\', '/'))
                for i2 in ds:
                    rets.append(str(os.path.join(r, i2)).replace('\\', '/'))
    except Exception as e:
        print(e)
    return rets


if __name__ == '__main__':
    try:
        pass  # 1,选择目标路径
        strDirPath = askdirectory()
        if len(strDirPath):
            pass  # 2,遍历目标路径下所有
            paths = traversal_dir(strDirPath)
            with open('temp.txt', 'w', encoding='utf8') as f:
                for path in paths:
                    f.write(path + '\n')
            os.system('temp.txt')
            os.chmod('temp.txt', stat.S_IWRITE)  # 剔除只读属性
            os.remove('temp.txt')
        pass
    except Exception as e:
        print(e)

Python.遍历目标路径下所有

标签:dia   director   imp   code   路径   只读   dial   pass   遍历   

原文地址:https://www.cnblogs.com/dailycode/p/12459781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!