Skip to content

Instantly share code, notes, and snippets.

@mintisan
Last active June 25, 2016 07:54
Show Gist options
  • Save mintisan/b9b8c759b2f397e0b2263e9eb52ee45d to your computer and use it in GitHub Desktop.
Save mintisan/b9b8c759b2f397e0b2263e9eb52ee45d to your computer and use it in GitHub Desktop.
modify file name: prefix, suffix, split and specific keywords
#-*- coding: utf-8 -*-
import os
import numpy as np
import walkdir as wd
file_info_type = np.dtype({
'names': ['dir','name'],
'formats':['S100','S100']
})
prefix_add = ''
keyword_remove = ''
old_split = '-'
new_split = '_'
old_suffix = 'txt'
new_suffix = 'csv'
# 0. get all file list
files = list(wd.file_paths(wd.filtered_walk(os.getcwd(),depth=1,included_files=['*.csv'])))
# 1. split dir & file
for x in xrange(0,len(files)):
s = '\\'
p = unicode(s.join(files[x].split('\\')[:-1]), 'gbk')
f = files[x].split('\\')[-1]
old = f
new = prefix_add + (f.replace(old_split, new_split)).replace(old_suffix, new_suffix)
new = new.replace(keyword_remove,'')
print p +': ' + old +' -> '+new
os.rename(p + '\\' + old,p + '\\' +new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment