Python argv文件,编辑,然后写入新文件

时间:2015-11-25 18:51:18

标签: python

使用Python,如果我想

一个。使用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行彻底失败了。

1 个答案:

答案 0 :(得分:2)

你说你有两个文件,一个你会读,一个你想写的新文件。您需要打开每个文件。

这个问题基本上与这个旧问题相同:edit text file using Python。您需要打开一个文件,读取它,打开另一个文件,然后写入它。