【发布时间】:2017-05-06 14:25:18
【问题描述】:
我编写了一个使用来自文本文件的值的 python 程序。为了获取文本文件,要求之一是它必须能够接受文件路径作为终端中的参数。我正在尝试使用:
# -*- coding: utf-8 -*-
import numpy
x = str(input("Enter directory path: \n"))
data = numpy.loadtxt(open(x), int)
但是,当我像这样在终端中运行程序时:
MBP:Game test$ python GameOfLife.py
Enter directory path:
/Users/test/Google Drive/Game.py
之后我收到以下错误。
Traceback (most recent call last):
File "Game.py", line 5, in <module>
x = str(input("Enter directory path\n"))
File "<string>", line 1
/Users/test/Google Drive/Game.py
^
SyntaxError: invalid syntax
我是 python 新手,所以任何帮助都会非常棒。
【问题讨论】:
-
你确定你使用的是 Python 3.X 吗?这看起来像是在 Python 2.X 上使用
input时会遇到的错误。 -
您没有使用 Python 3。输入字符串得到
evaled,这是 Python 2input()函数的行为。 -
“在终端中接受文件路径作为参数”到底是什么意思?用户应该运行程序然后输入路径,还是应该在启动程序时提供路径,例如
py Game.py path/dir/file.py? -
我正在使用 Python3。它们在启动程序时提供路径。谢谢
标签: python python-3.x terminal command-line-arguments