Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python 2.7进行内部参数解析_Python_Python 2.7_Args - Fatal编程技术网

使用Python 2.7进行内部参数解析

使用Python 2.7进行内部参数解析,python,python-2.7,args,Python,Python 2.7,Args,在读取/写入输入文件时,我需要在“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

在读取/写入输入文件时,我需要在“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”也一样。有什么解决办法吗

非常感谢,

Romain刚刚发现了错误:

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
替换为
args.input
@c3st7n:谢谢,我在那里有一个逗号,这就是为什么不起作用的原因。kay:)我不知道您为什么要麻烦使用格式字符串,因为您只是从字面上获取
args.input
的值,而没有添加任何内容。