Skip to content

Instantly share code, notes, and snippets.

@kashefy
Created June 16, 2015 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kashefy/056a0db2f5600cf2b68a to your computer and use it in GitHub Desktop.
Save kashefy/056a0db2f5600cf2b68a to your computer and use it in GitHub Desktop.
traversing subdirectories and renaming image files with strange windows-specific character
'''
Created on Jun 16, 2015
@author: kashefy
'''
import argparse
import os
def list_paths(root_path):
all_paths = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(root_path)
for f in files if f.endswith('.tif') or f.endswith('.jpg')]
return all_paths
def new_fname(paths_):
paths_new = [fpath.replace('\xb0', '') for fpath in paths_]
return paths_new
def main(args):
dir_input = args.input
if not os.path.isdir(dir_input):
raise IOError("Inut directory does not exist (%s)" % dir_input)
paths_ = list_paths(dir_input)
print '%d files found' % len(paths_)
paths_new = new_fname(paths_)
count_renamed = 0
for src, dst in zip(paths_, paths_new):
if src != dst:
os.rename(src, dst)
count_renamed += 1
print 'renamed %d files' % count_renamed
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("input", type=str,
help="Input directory")
args = parser.parse_args()
ret = main(args)
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment