Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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.4中打开(sys.argv[]的文件_Python_Python 2.7 - Fatal编程技术网

无法读取Python 2.7.4中打开(sys.argv[]的文件

无法读取Python 2.7.4中打开(sys.argv[]的文件,python,python-2.7,Python,Python 2.7,我在Mac(OS Lion)终端的Python环境中 python脚本包含以下行: def main(): file1 = open(sys.argv[1]) file2 = open(sys.argv[2]) file3 = open(sys.argv[3]) 我假设我需要通过执行以下操作来运行脚本: script.py file1.txt file2.txt file3.txt 但我一直收到下面的错误消息: >>> process.py outp

我在Mac(OS Lion)终端的Python环境中

python脚本包含以下行:

def main():
    file1 = open(sys.argv[1])
    file2 = open(sys.argv[2])
    file3 = open(sys.argv[3])
我假设我需要通过执行以下操作来运行脚本:

script.py file1.txt file2.txt file3.txt
但我一直收到下面的错误消息:

>>> process.py output1.txt output2.txt output3.txt
  File "<stdin>", line 1
    process.py output1.txt output2.txt output3.txt
                     ^
SyntaxError: invalid syntax
>>process.py output1.txt output2.txt output3.txt
文件“”,第1行
process.py output1.txt output2.txt output3.txt
^
SyntaxError:无效语法

所有文件和脚本都在当前工作目录中(我通过导入操作系统检查了它,并运行了打印操作系统.getcwd()。有人能给我指出正确的方向吗?谢谢!

您不能从python本身运行python脚本

您需要从命令行启动脚本:

 python script.py file1.txt file2.txt file3.txt

使用命令提示符或bashshell


python scriptName.py

从提示符处看,似乎您正试图从python解释器中运行脚本,即键入
python
,然后在python
>
提示符处输入脚本名称和参数。该提示符用于输入脚本名称和参数以交互方式调用实际的Python命令


如果退出Python解释器,只需在正常的shell提示符下键入脚本名称和参数,就可以了(只要脚本的第一行是
#!/usr/bin/Python

def main(*argv):
    file1 = open(argv[1])
    file2 = open(argv[2])
    file3 = open(argv[3])

if __name__ == "__main__":
    main(*sys.argv)
这为您提供了更大的灵活性,允许您从命令行调用程序,但也可以在以下程序中调用:

import process
process.main("output1.txt", "output2.txt", "output3.txt")

这难道不应该说“您不能从python本身运行带有sys.arguments的python脚本吗?”好吧,除非
eval
和friends,否则您不能运行python脚本。
sys.argv
对于OP做了什么并不重要,只要您尝试
execfile
它就会起作用。