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
Python在命令行参数中占用额外空间_Python_Python 2.7 - Fatal编程技术网

Python在命令行参数中占用额外空间

Python在命令行参数中占用额外空间,python,python-2.7,Python,Python 2.7,当我在python中有以下代码部分时,假设我们使用输入文件input.txt作为第一个命令行参数来执行这个python文件 import os import sys with open(sys.argv[1],"r") as input: 假设python文件的名称是file.py,而input.txt是该文件的参数,因此命令如下: >>file.py input.txt 我得到以下错误: Traceback (most recent call last): File "C:\U

当我在python中有以下代码部分时,假设我们使用输入文件input.txt作为第一个命令行参数来执行这个python文件

import os
import sys
with open(sys.argv[1],"r") as input:
假设python文件的名称是file.py,而input.txt是该文件的参数,因此命令如下:

>>file.py input.txt
我得到以下错误:

Traceback (most recent call last):
File "C:\Users\hdutta\Desktop\file.py", line 3, in <module>
with open(sys.argv[1],"r") as input:
IOError: [Errno 2] No such file or directory: ' input.txt'
回溯(最近一次呼叫最后一次):
文件“C:\Users\hdutta\Desktop\File.py”,第3行,在
以open(sys.argv[1],“r”)作为输入:
IOError:[Errno 2]没有这样的文件或目录:“input.txt”
看起来它在第一个参数处占用了额外的空间。为什么会这样?我错过了什么吗。这是以前的工作。但我现在有了一个新的系统,我面临着这个问题

我在链接中找到了一些线索--

但是我无法修改sys.argv=,如果有办法的话

嗯,很奇怪

但你可以做到:

sys.argv[1].lstrip().strip() # Will remove any spaces to left / right of the string. 
嗯,奇怪

但你可以做到:

sys.argv[1].lstrip().strip() # Will remove any spaces to left / right of the string. 

你在用什么shell?我在用Windows 10 64位你在用什么shell?我在用Windows 10 64位你不需要
lstrip
。只需
strip
就可以了,我可以为我添加的这个file.py做这件事,但是不可能为我正在使用的一些第三个python文件实现,这是获取命令行参数的基本问题。我想问题是我在设置HKEY_CLASSES\u ROOT\Applications\python.exe\shell\open\command:with
“%*”
。当删除
时,问题就解决了,您不需要
lstrip
。只需
strip
就可以了,我可以为我添加的这个file.py做这件事,但是不可能为我正在使用的一些第三个python文件实现,这是获取命令行参数的基本问题。我想问题是我在设置HKEY_CLASSES\u ROOT\Applications\python.exe\shell\open\command:with
“%*”
。当删除
时,问题得到解决