Last active
August 29, 2015 14:03
-
-
Save craigpalermo/463fa62e17bee9e3c77f to your computer and use it in GitHub Desktop.
This script moves the largest file in each folder in the directory that it's run from into that directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
This script moves the largest file from each subdirectory into the | |
same directory that the script is run from. | |
Before: | |
/root | |
/episode1 | |
e1.avi | |
... | |
/episode2 | |
e2.avi | |
... | |
After: | |
/root | |
e1.avi | |
e2.avi | |
''' | |
import os, os.path | |
import shutil | |
def get_largest_file(dirtocheck): | |
''' | |
Returns the largest file inside dirtocheck, or None if no files exist. | |
''' | |
largest = None | |
for root, _, files in os.walk(dirtocheck): | |
for f in files: | |
fullpath = os.path.join(root, f) | |
if largest == None or os.path.getsize(fullpath) > os.path.getsize(largest): | |
largest = fullpath | |
return largest | |
# Get the current directory the script is being run in | |
current = os.path.dirname(os.path.realpath(__file__)) | |
# Iterate through folders in the current directory. Each folder should contain | |
# on episode, which will be moved to the current directory. After moving, delete | |
# the folder it was moved from. | |
for subdir, dirs, files in os.walk(current): | |
# Ignore subdir if it's the same as current | |
if subdir != current: | |
largest = get_largest_file(subdir) | |
shutil.move(largest, os.getcwd()) | |
shutil.rmtree(subdir) | |
# remove script file when finished | |
os.remove("move_videos.py") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment