Created
May 31, 2014 08:34
-
-
Save raspi/2a2abd95ba5c2444cd93 to your computer and use it in GitHub Desktop.
Remove file from 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 -*- | |
# Remove file from directory if it's the only one there including sub-directories. | |
# usage: script.py <file to find> <directory> | |
# script.py readme.txt /stuff/files/misc | |
import os | |
import sys | |
fn = None | |
if len(sys.argv) > 1: | |
fn = sys.argv[1] | |
if fn == "": | |
fn = None | |
if fn is None: | |
print "invalid filename" | |
sys.exit(1) | |
topdir = sys.argv[2] | |
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 files[0].lower() == fn.lower() and len(dirnames) == 0: | |
filename = os.path.join(dirpath, files[0]) | |
os.remove(filename) | |
print "Removed: " + filename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment