我正在为下载的电视节目编写一个清理脚本。我希望它获得每个文件夹中最大的文件,移动/重命名它,然后删除该文件夹。我遇到的问题是,有时当有另一个嵌套文件夹时,它会崩溃并跳过它。我不知道如何将它转换成具有相同功能的递归函数。如果只查找最大的文件而不是使用硬编码的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)
我目前正在学习python:)感谢您的练习:D
这应该是有效的:
祝你好运!在
相关问题 更多 >
编程相关推荐