Skip to content

Instantly share code, notes, and snippets.

@theand
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save theand/9072553 to your computer and use it in GitHub Desktop.

Select an option

Save theand/9072553 to your computer and use it in GitHub Desktop.
walks in to directory from cwd, then move all files to parent directory. also check name duplication.
#-*- coding: utf-8 -*-
import os
import os.path
import shutil
for dirpath, dirnames, filenames in os.walk('.'):
for filename in filenames:
if filename == "extract.py" or filename.startswith('.'):
continue
src = os.path.join(dirpath, filename)
if not os.path.exists(src):
print 'no exist : ' + src
continue
if not os.path.isfile(src):
print 'not file : ' + src
continue
dest = '../' + filename
name, ext = os.path.splitext(dest)
i = 1
while 1:
if os.path.exists(dest):
print 'existing dest'
dest = "%s (%d)%s" %(name, i, ext)
print 'change to %s' % dest
i+=1
continue
else:
break
shutil.copyfile(src, dest)
print 'copy - ' + filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment