Maison >développement back-end >Tutoriel Python >python修改文件
#!/usr/bin/env python #coding=utf-8 import os import sys pubid0=0000000 pubid1=1111111 path=1111111 def get_filenames(fdir): return os.listdir(fdir) def rename_file(filename,pubid0,pubid1): os.rename(filename,filename.replace(pubid0,pubid1)) if __name__ == '__main__': path = sys.argv[1] pubid0 = sys.argv[2] pubid1 = sys.argv[3] print '\t path :',path,'\t pubid0:',pubid0,'\t pubid1:',pubid1 print '\t\t***************************************\n' filesname = get_filenames(path) for i in filesname: # print i ,os.path.join(path,i) nPos =str(i).find(pubid0) if nPos > 0: rename_file(os.path.join(path,i),pubid0,pubid1) print os.path.join(path,i)