Skip to content

Instantly share code, notes, and snippets.

@hasherezade
Last active June 2, 2016 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hasherezade/bfe5387c6aed8a352f88 to your computer and use it in GitHub Desktop.
Save hasherezade/bfe5387c6aed8a352f88 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"Copies/moves files from the list into the separate folder, CC-BY: hasherezade"
__VERSION__ = "0.1"
import sys
import os
import re
import argparse
def get_names(fname):
names = set()
with open(fname, 'r') as f:
for line in f.readlines():
line = line.strip()
names.add(line)
return names
def remove_ext(filename):
index = filename.find('.')
if index == (-1):
return filename
return filename[0:index]
def get_matching_files(names, dir_content):
matching = set()
for fname in dir_content:
noext = remove_ext(fname)
if noext in names:
matching.add(fname)
continue
for name in names:
if fname.find(name) != (-1):
#print "%s , %s , %d" % (noext, name, index)
matching.add(fname)
break
return matching
def is_windows():
from sys import platform as _platform
if "win" in _platform:
return True
return False
def get_move_command():
command = "mv"
if is_windows():
command = "move"
return command
def get_copy_command():
command = "cp"
if is_windows():
command = "copy"
return command
def copy_found(found, indir, outdir, is_move=False):
command = get_copy_command()
if is_move == True:
command = get_move_command()
print "moving files!"
for filen in found:
path1 = indir + "/" + filen
path2 = outdir + "/" + filen
os.system(command + " " + path1 + " " + path2)
def main():
parser = argparse.ArgumentParser(description="File sieve "+ __VERSION__)
parser.add_argument('--files', dest="files", default=None, help="Input file with list of files to be copied/moved", required=True)
parser.add_argument('--indir', dest="indir", default=None, help="Input directory with list of files to be copied/moved", required=True)
parser.add_argument('--outdir', dest="outdir", default=None, help="Output directory where the files should be copied/moved", required=True)
parser.add_argument('--move', dest="move", default="False", action='store_true', help="Move instead of coping?")
args = parser.parse_args()
names = get_names(args.files)
dir_content = set(os.listdir(args.indir))
matching = get_matching_files(names, dir_content)
copy_found(matching, args.indir, args.outdir, args.move)
print "Names: %d" % len(names)
print "Files: %d" % len(dir_content)
print "Found: %d" % len(matching)
print "----"
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment