“Python 目录管理”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
(创建页面,内容为“==引入模块== import os ==具体操作== ===查看当前目录=== <nowiki>>>> os.getcwd() 'C:\\Users\\maxin\\AppData\\Local\\Programs\\Python\\Python37' >>></no…”)
 
第50行: 第50行:
 
  <nowiki>['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll']</nowiki>
 
  <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
  
     1.2 切换目录 - cd 命令
+
>>> for fname in os.listdir('.'):
     1.3 创建目录 - mkdir命令
+
 
     1.4 显示目录内容 - ls命令
+
sub_path = os.path.join('.', fname)
         1.4.1 显示隐藏文件
+
 
         1.4.2 显示目录本身属性
+
if os.path.isfile(sub_path):
         1.4.3 显示特定文件
+
 
         1.4.4 定义命令别名
+
size =(os.path.getsize(sub_path))/1024 #换算成KB
     1.5 查看目录空间占用情况 - du命令
+
 
     1.6 删除目录命令 - rmdir 命令
+
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>

2020年3月29日 (日) 04:57的版本

引入模块

import os

具体操作

查看当前目录

>>> os.getcwd()
'C:\\Users\\maxin\\AppData\\Local\\Programs\\Python\\Python37'
>>> 

创建目录

os.mkdir(os.getcwd()+'\\temp')

切换目录

>>> os.chdir(os.getcwd()+'\\temp')

>>> os.getcwd()

'C:\\Users\\maxin\\AppData\\Local\\Programs\\Python\\Python37\\temp'

>>> os.chdir('..')

显示目录内容

>>> os.listdir('.')

['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'temp', 'Tools', 'vcruntime140.dll']

>>> [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)


LICENSE.txt
NEWS.txt
python.exe
pythonw.exe

删除目录

>>> os.listdir('.')

['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'temp', 'Tools', 'vcruntime140.dll']

>>> os.rmdir('temp')

>>> os.listdir('.')

['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll']

查看目录空间占用情况

>>> 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")


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

应用:统计目录占用空间

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)