0
我正在为下载的电视节目制作清理脚本。我希望它获得每个文件夹中最大的文件,移动/重命名它,然后删除该文件夹。我遇到的问题是,有时当有另一个嵌套的文件夹时,它会崩溃并跳过它。我不知道如何将其转换为具有相同功能的递归函数。只是寻找最大的文件,而不使用硬编码的30MB也是很好的。查找最大文件和删除文件夹
此外,有时我下载的文件有不正确的日期,所以如果它可以使每个新文件成为当前脚本运行的日期和时间,那将是非常好的。
import os
import shutil
dir = "C:\Users\Bobe\Downloads\TV\\"
for folder in os.listdir(dir):
if os.path.isdir(os.path.join(dir,folder)):
for file in os.listdir(dir + folder):
filelocation = dir+folder+"\\"+file
if os.path.getsize(filelocation) > 30000000: # This is in bytes (30 MB)
extension = os.path.splitext(file)[1]
shutil.move(filelocation, dir + folder + extension)
else:
os.remove(filelocation)
shutil.rmtree(dir + folder)
`无'是一个比'-1'更好的特殊值,意思是“未初始化”,你可以比较'不是'。 – nmichaels 2010-11-27 03:22:58