标签:file
1.tuple:元组 即是常量数组
格式:(‘a‘,‘b‘,‘c‘...)
sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始
将filename的old_txt字符替换为new_txt字符
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys,os if len(sys.argv) <= 4: print "usage:./file_replace.py old_txt nex_txt filename" file_name = sys.argv[3] old_txt, new_txt = sys.argv[1],sys.argv[2] print file_name f = file(file_name,‘rb‘) new_file = file(‘%s.bak‘ % file_name,‘wb‘) for line in f.readlines(): print line.replace(old_txt,new_txt) new_file.write(line.replace(old_txt,new_txt)) f.close() new_file.close()
本文出自 “12350221” 博客,请务必保留此出处http://12360221.blog.51cto.com/12350221/1917953
标签:file
原文地址:http://12360221.blog.51cto.com/12350221/1917953