使用Python,如果我想Python的argv的文件,编辑,然后写入新文件
A.读取文本文件中使用的argv
B.使字符串替换(搜索&替换)
C.写用新名称创建一个新文件
我可以使用一个open()函数吗?这就是我到目前为止,是的,我知道它不会按原样运行。
import sys, re
FarmFixer, farmfile = argv
print "What is the serial number of the site?",
_nnn = raw_input()
print "What is the brand, or product name?",
_brand = raw_input()
print "What is the (fqdn) ServerName?",
_server_name = raw_input()
print "What is the content path?",
_content_path = raw_input()
print "What is the DAM path?",
_dampath = raw_input()
print "Which environment is this for?",
_env = raw_input()
print "What is the cache document root?",
_cache_docroot = raw_input()
for line in file_open:
re.sub("NNN", "_nnn", line)
re.sub("BRAND", "_brand", line)
re.sub("CONTENT_PATH", "_content_path", line)
re.sub("DAMPATH", "_dampath", line)
re.sub("ENV", "_env", line)
re.sub("CACHE_DOCROOT", "_cache_docroot", line)
farmfile = _nnn + _brand + "farm.any"
outie = open(farmfile, 'w')
outie.close
print farmfile
当然re.sub行失败了。
现在不要介意破损的re.sub行。 –
建议:使用'with'语句来处理关闭文件。发生错误更安全,编码更简单。您发布的代码不会调用close方法。 – dsh
'outie.close'不关闭文件。 'outie.close()'确实。 –