使用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()'確實。 –