Created
June 16, 2015 13:03
-
-
Save kashefy/056a0db2f5600cf2b68a to your computer and use it in GitHub Desktop.
traversing subdirectories and renaming image files with strange windows-specific character
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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