Created
December 28, 2016 21:29
-
-
Save raspi/35d52b293e3baf41d3a4a5307dec125e to your computer and use it in GitHub Desktop.
Move file to parent directory if it's the only one there including sub-directories.
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
#!/bin/env/python | |
# -*- encoding: utf8 -*- | |
# Move file to parent directory if it's the only one there including sub-directories. | |
# usage: script.py <directory> | |
# script.py /stuff/files/ | |
import os | |
import sys | |
from shutil import move | |
if len(sys.argv) <= 1: | |
print("Directory not given") | |
sys.exit(1) | |
topdir = os.path.abspath(sys.argv[1]) | |
if not os.path.isdir(topdir): | |
print("invalid directory") | |
sys.exit(1) | |
for dirpath, dirnames, files in os.walk(topdir): | |
if len(files) == 1 and len(dirnames) == 0: | |
filename = os.path.join(dirpath, files[0]) | |
parentdir = os.path.dirname(dirpath) | |
if os.path.isdir(parentdir): | |
print("Moving '{0}' to {1}".format(filename, parentdir)) | |
move(filename, parentdir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment