Skip to content

Instantly share code, notes, and snippets.

@shadowfax92
Last active August 29, 2015 14:17
Show Gist options
  • Save shadowfax92/d9e8e126680b720134d6 to your computer and use it in GitHub Desktop.
Save shadowfax92/d9e8e126680b720134d6 to your computer and use it in GitHub Desktop.
#! /usr/bin/python
import sys
import os
import re
import time
import shutil
def get_all_files(dir):
files = dict()
for dirName, subdirList, fileList in os.walk(dir):
print('Found directory: %s' % dirName)
for fname in fileList:
print('\t%s' % fname)
_path = dirName+'/'+fname
if _path not in files:
files[_path] = dict()
files[_path]["name"] = fname
# files[_path]["dir"] = dirName
files[_path]["dir"] = dirName[-3:]
print files[_path]
return files
def format_files(out_dir, files):
# create/check output directory
try:
os.stat(out_dir)
except:
print 'Output director', out_dir, 'is not present. Creating it!'
os.mkdir(out_dir)
for file in files.keys():
# print "created: %s" % time.ctime(os.path.getctime(file))
print "file name: ", str(files[file]["name"]), "dir: ", str(files[file]["dir"]), " last modified: ", str(time.ctime(os.path.getmtime(file)))
_time = time.ctime(os.path.getmtime(file))
parsed = time.strptime(_time)
lm_time = time.strftime("%H-%M-%S", parsed)
new_file_name = lm_time + '_' + files[file]["dir"] + '_' + files[file]["name"]
print new_file_name
shutil.copy2(file, out_dir + '/' + new_file_name)
def main():
if len(sys.argv) == 3:
cam_dir_parse = sys.argv[1]
output_dir = sys.argv[2]
else:
print "Expects command line arguments. Cam directory and Output directory"
sys.exit(1)
files = get_all_files(cam_dir_parse)
format_files(output_dir, files)
if __name__ == "__main__":
main()
@shadowfax92
Copy link
Author

executing:
python cam_folder_organizer.py ~/casper/cam1 ./tmp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment