I’m working on a cleanup script for tv shows that I download. I want it to get the largest file in each folder, move/rename it and then delete that folder. The issue I’m having is that sometimes when there is another nested folder, it crashes and skips it. I’m not sure how to convert this into a recursive function that does the same functionality. It would also be nice to just look for the largest file instead of use the hard coded 30MB.
Also, sometimes a file I download has an incorrect date so it would be great if it could make each new file the current date and time when the script ran.
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)
I’m currently learning python 🙂 Thanks for the exercise 😀
This should work:
Good luck!