【问题标题】:Python 3 - open file from consolePython 3 - 从控制台打开文件
【发布时间】: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")

标签: python file path


【解决方案1】:

引号。

txt = open("C:\\user\\documents\\python\\ex15_sample.txt")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-07
    • 2016-11-28
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    相关资源
    最近更新 更多