Skip to content

Instantly share code, notes, and snippets.

@polymorphm
Last active December 19, 2015 18:29
Show Gist options
  • Save polymorphm/5999297 to your computer and use it in GitHub Desktop.
Save polymorphm/5999297 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- mode: python; coding: utf-8 -*-
assert str is not bytes
import sys, os, os.path
def del_prefix(s, prefix):
assert isinstance(s, str)
assert isinstance(prefix, str)
if not s.startswith(prefix):
return s
return s[len(prefix):]
def main():
assert len(sys.argv) == 3
src_dir = sys.argv[1]
dst_dir = sys.argv[2]
os.mkdir(dst_dir)
schedule = []
root_prefix = None
for root, dirs, files in os.walk(src_dir):
if root_prefix is None:
root_prefix = root
new_root = root
new_root = del_prefix(new_root, root_prefix)
new_root = del_prefix(new_root, '/')
new_root = new_root.replace('.', '_').replace(' ', '_')
for filename in files:
path = os.path.join(root, filename)
new_filename = '{}__{}'.format(new_root.replace('/', '__'), filename.replace(' ', '_'))
new_path = os.path.join(dst_dir, new_filename)
print('schedule: {!r} => {!r}...'.format(path, new_path), end=' ')
schedule.append((path, new_path))
print('OK!')
for path, new_path in schedule:
print('rename: {!r} => {!r}...'.format(path, new_path), end=' ')
os.rename(path, new_path)
print('OK!')
print('DONE!')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment