在读取/写入文件时,我需要在“ with”函数中为输入文件插入变量。这是我当前感兴趣的部分代码:
import argparse
parser=argparse.ArgumentParser(description="My script")
parser.add_argument('-i','--input',help='Input log file name',required=True)
parser.add_argument('-o','--output',help='Desired name for the Excel file',required=True)
parser.add_argument('-s','--sheet',help='Desired name of the Excel sheet(Default: Sheet1)',default='Sheet1',required=False)
args=parser.parse_args()
with open('%s',%args.input, 'r') as file :
filedata = file.read()
filedata = filedata.replace('destination', 'destination xxx')
with open('%s',%args.input, 'w') as file:
file.write(filedata)
'%s',%args.input, 'r'
无效,但是我需要做一些事情。 “ w”相同。有什么解决方法吗?
非常感谢,
罗曼
答案 0 :(得分:0)
刚发现错误:
import argparse
parser=argparse.ArgumentParser(description="My script")
parser.add_argument('-i','--input',help='Input log file name',required=True)
parser.add_argument('-o','--output',help='Desired name for the Excel file',required=True)
parser.add_argument('-s','--sheet',help='Desired name of the Excel sheet(Default: Sheet1)',default='Sheet1',required=False)
args=parser.parse_args()
with open('%s' %args.input, 'r') as file :
filedata = file.read()
filedata = filedata.replace('destination', 'destination xxx')
with open('%s' %args.input, 'w') as file:
file.write(filedata)