Argparse 里面有 Python 2.7

Arg parse inside with Python 2.7

我需要在 'with' 函数中为输入文件插入变量,而 reading/writing 在其中。这是我目前感兴趣的代码部分:

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' 也一样。任何解决方法?

非常感谢,

罗曼

刚发现错误:

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)