python modify files

巴扎黑
巴扎黑Original
2016-12-09 14:28:281470browse

   #!/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)


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Python learning--moduleNext article:Python learning--module