这里分享一段python代码批量修改文件名字:
import os,sys,re mydir=os.path.abspath(sys.argv[1]) os.chdir(mydir) cwd = os.getcwd() #group ID map id={"A79_A103_A104_A246_A356_A357_A375_A378_A379_A381_vs_A37_A409_A5_A48_A32_A221_A41_A241_A406_A408":"CW_vs_AW", "A11_A12_A13_A19_A201_A211_A271_A303_A314_A315_A316_A318_A319_A325_A329_A337_A341_A344_A346_A349_A351_A360_A370_vs_A5_A32_A37_A41_A48_A79_A103_A104_A221_A241_A246_A356_A357_A375_A378_A379_A381_A406_A408_A409":"C_vs_W" } i=0 for dir_path, dir_names, file_names in os.walk(cwd): for file_name in file_names: p=os.path.join(dir_path,file_name) if len(file_name)>100: m=re.match(r'(.+_vs_[^\.]+)', file_name) vs=m.group(1) if(id.has_key(vs)): newfilename=re.sub(r'^[^\.]+_vs_[^\.]+\.', "%s."%str(id[vs]), file_name) os.system("mv %s %s"%(p,os.path.join(dir_path,newfilename))) print(os.path.basename(p)+"\t"+newfilename) else: i=i+1 id[vs]="DEG%s"%i newfilename=re.sub(r'^[^\.]+_vs_[^\.]+\.', "%s."%str(id[vs]), file_name) os.system("mv %s %s"%(p,os.path.join(dir_path,newfilename))) print(os.path.basename(p)+"\t"+newfilename)
更多可学习python课程:python语言入门到精通
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!