Skip to content

Instantly share code, notes, and snippets.

@tzengerink
Last active August 29, 2015 14:16
Show Gist options
  • Save tzengerink/e69d8684d1f38ed7b6f4 to your computer and use it in GitHub Desktop.
Save tzengerink/e69d8684d1f38ed7b6f4 to your computer and use it in GitHub Desktop.
Store each extension in a directory, in a seperate folder within that directory
#!/usr/bin/env python
'''
REORGANIZE
----------
When your directory looks something like this:
/path/to/dir/photo-1.jpg
/path/to/dir/photo-2.JPG
/path/to/dir/video-1.mp4
/path/to/dir/video-2.MP4
/path/to/dir/photo-1.NEF
/path/to/dir/photo-2.NEF
and you run this script by executing
$> ./reorganize.py /path/to/dir
then your directory will look like this:
/path/to/dir/jpg/photo-1.jpg
/path/to/dir/jpg/photo-2.JPG
/path/to/dir/mp4/video-1.mp4
/path/to/dir/mp4/video-2.MP4
/path/to/dir/nef/photo-1.nef
/path/to/dir/nef/photo-2.NEF
- - -
Copyright (c) 2015 Teun Zengerink
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
import glob
import os
import sys
def main(rootdir):
for file in glob.iglob('%s/*.*' % rootdir):
name, extension = os.path.splitext(os.path.basename(file))
subdir = extension[1:].lower()
try:
os.makedirs('%s/%s' % (rootdir, subdir))
except:
pass
os.rename('%s/%s%s' % (rootdir, name, extension),
'%s/%s/%s%s' % (rootdir, subdir, name, extension))
if __name__ == '__main__':
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment