Skip to content

Instantly share code, notes, and snippets.

@raspi
Created December 28, 2016 21:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raspi/35d52b293e3baf41d3a4a5307dec125e to your computer and use it in GitHub Desktop.
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.
#!/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