【发布时间】:2015-08-27 16:45:13
【问题描述】:
我正在尝试从终端中的 python 控制台对文件运行 open() 命令,而不是通过运行脚本。例如,我将键入以下内容以将文件分配给变量“txt”:
>>> txt = open(C:\\user\\documents\\python\\ex15_sample.txt)
这是输出:
File "<stdin>", line 1
txt = open(C:\\user\\documents\\python\\ex15_sample.txt)
^
SyntaxError: invalid syntax
如何修改此命令以成功打开所列路径的文件?
【问题讨论】:
-
呃,用引号吗?
-
你需要用引号包裹你的路径:
txt = open("C:\\user\\documents\\python\\ex15_sample.txt")