我一次又一次收到此错误,请告诉我为什么

时间:2019-11-27 17:42:15

标签: python

我正在juypternotebook中创建一个txt文件

%%writefile test.txt
hello this is my first line
hello this is my second line 

之后,当我通过编写代码在该txt文件中添加一些行时

with open ('test.txt' ,mode = 'r') as f:

然后出现此错误

File "<ipython-input-9-e008d94f1356>", line 1
    with open ('test.txt' ,mode = 'r') as f:
                                            ^
SyntaxError: unexpected EOF while parsing

请告诉我为什么

1 个答案:

答案 0 :(得分:2)

您不能启动with语句(或任何块)并仅在此处结束脚本,该块中至少需要一个命令。

如果您不想添加命令,请使用pass作为占位符。

with open ('test.txt' ,mode = 'r') as f:
    pass