python遍歷文件夾讀取文件大小


閑來無事,寫了個小程序刪除內存卡中大於50m的文件

# filename  itertaorfilefolder
import os
import os.path

filePath = raw_input('Enter filepath : ')

#遍歷文件夾
#三個參數:分別返回1.父目錄 2.所有文件夾名字(不含路徑) 3.所有文件名字
for parent ,dirnames , filenames in os.walk(filePath):
    #輸出文件夾信息
    for dirname in dirnames:
        print 'parent is :'+parent
        print 'dirname is '+ dirname
    #輸出文件信息
    for filename in filenames :
        print 'parent is :'+parent
        print 'filename is :' + filename
        #輸出文件路徑信息
        currentPath = os.path.join(parent,filename)
        print 'the fulll name of the file is :'+ currentPath
        filesize = os.path.getsize(currentPath)/1024/1024
        print 'the file size is : %.3f MB' %(filesize)
        #刪除大於50m的文件
        if filesize > 50:
            delete = raw_input(' are you sure to delete ?')
            if delete == 'yes':
                os.remove(currentPath)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM