查看“Python 目录管理”的源代码
←
Python 目录管理
跳转至:
导航
,
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
==引入模块== import os ==具体操作== ===查看当前目录=== <nowiki>>>> os.getcwd() 'C:\\Users\\maxin\\AppData\\Local\\Programs\\Python\\Python37' >>></nowiki> ===创建目录=== os.mkdir(os.getcwd()+'\\temp') 创建多级目录 ?os.makedirs(r“c:\python\test”) ===切换目录=== >>> os.chdir(os.getcwd()+'\\temp') >>> os.getcwd() 'C:\\Users\\maxin\\AppData\\Local\\Programs\\Python\\Python37\\temp' >>> os.chdir('..') ===遍历目录内容=== >>> os.listdir('.') <nowiki>['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'temp', 'Tools', 'vcruntime140.dll']</nowiki> >>> [fname for fname in os.listdir('.')if fname.endswith(('.exe', '.txt'))] ['LICENSE.txt', 'NEWS.txt', 'python.exe', 'pythonw.exe'] >>> for fname in os.listdir('.'): if fname.endswith(('.exe', '.txt')): print(fname) <nowiki>LICENSE.txt NEWS.txt python.exe pythonw.exe</nowiki> 应用:[[Python实例:制作图片电子书]] ===遍历目录下所有子目录和文件=== ====深度遍历==== <nowiki>from os import listdir from os.path import join, isfile, isdir def listDirDepthFirst(directory): '''深度优先遍历文件夹''' #遍历文件夹,如果是文件就直接输出 #如果是文件夹,就输出显示,然后递归遍历该文件夹 for subPath in listdir(directory): path = join(directory, subPath) if isfile(path): print(path) elif isdir(path): print(path) listDirDepthFirst(path)</nowiki> ====广度遍历==== 下面的代码使用了广度优先遍历方法。 <nowiki>from os import listdir from os.path import join, isfile, isdir def listDirWidthFirst(directory): '''广度优先遍历文件夹''' #使用列表模拟双端队列,效率稍微受影响,不过关系不大 dirs = [directory]#这个directory就是列表的第一个元素 #如果还有没遍历过的文件夹,继续循环 while dirs: #遍历还没遍历过的第一项 current = dirs.pop(0) #遍历该文件夹,如果是文件就直接输出显示 #如果是文件夹,输出显示后,标记为待遍历项 for subPath in listdir(current): path = join(current, subPath) if isfile(path): print(path) elif isdir(path): print(path) dirs.append(path) </nowiki> ===删除目录=== >>> os.listdir('.') <nowiki>['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'temp', 'Tools', 'vcruntime140.dll']</nowiki> >>> os.rmdir('temp') >>> os.listdir('.') <nowiki>['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll']</nowiki> ===查看目录空间占用情况=== >>> os.path.getsize('pythonw.exe') 98320 >>> for fname in os.listdir('.'): sub_path = os.path.join('.', fname) if os.path.isfile(sub_path): size =(os.path.getsize(sub_path))/1024 #换算成KB print(fname,":",size,"KB") <nowiki>LICENSE.txt : 29.48046875 KB NEWS.txt : 703.82421875 KB python.exe : 97.515625 KB python3.dll : 57.515625 KB python37.dll : 3663.015625 KB pythonw.exe : 96.015625 KB vcruntime140.dll : 87.6484375 KB</nowiki> ===比较两个目录=== ==应用:统计目录占用空间== <nowiki>import os totalSize = 0 fileNum = 0 dirNum = 0 def visitDir(path): global totalSize global fileNum global dirNum for lists in os.listdir(path): sub_path = os.path.join(path, lists) if os.path.isfile(sub_path): fileNum = fileNum+1 #统计文件数量 totalSize = totalSize+os.path.getsize(sub_path) #统计文件总大小 elif os.path.isdir(sub_path): dirNum = dirNum+1 #统计文件夹数量 visitDir(sub_path) #递归遍历子文件夹 def main(path): if not os.path.isdir(path): print('Error:"', path, '" is not a directory or does not exist.') return visitDir(path) def sizeConvert(size): #单位换算 K, M, G = 1024, 1024**2, 1024**3 if size >= G: return str(size/G)+'G Bytes' elif size >= M: return str(size/M)+'M Bytes' elif size >= K: return str(size/K)+'K Bytes' else: return str(size)+'Bytes' def output(path): print('The total size of '+path+' is:'+sizeConvert(totalSize) +'('+str(totalSize)+' Bytes)') print('The total number of files in '+path+' is:',fileNum) print('The total number of directories in '+path+' is:',dirNum) if __name__=='__main__': path = r'D:\teaching\自动化运维' main(path) output(path) </nowiki> ==应用:复制空目录== 云盘备份:有的时候需要将云端的目录结构复制到本地
返回至
Python 目录管理
。
导航菜单
个人工具
登录
命名空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息