hhhhhhy 发表于 2020-7-25 16:48:25

with open as f出现SyntaxError

#035 作业2
import easygui as g

file_path = g.fileopenbox()
file1 = open(file_path,'r',encoding = 'UTF-8')
#with open(file_path,'r',encoding = 'UTF-8') as file1
content = []
for each_line in file1:
    content.append(each_line)
g.textbox(text=content,title='显示文件内容',msg='文件'+file_path+'的内容显示如下:')

读文件时,用file1 = open()没问题,用with open as f(如下一行的注释所示)就会报错,请问为什么?

使用平台及版本:3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41)
python版本:Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) on win32
(问题出现在035讲的课后题,之前用with open as f 没问题)

hrp 发表于 2020-7-25 17:24:20

这是语法问题
# coding:utf8

#035 作业2
import easygui as g

file_path = g.fileopenbox()
#file1 = open(file_path,'r',encoding = 'UTF-8')
with open(file_path,'r',encoding = 'UTF-8') as file1:
    content = []
    for each_line in file1:
      content.append(each_line)
g.textbox(text=content,title='显示文件内容',msg='文件'+file_path+'的内容显示如下:')

hhhhhhy 发表于 2020-7-25 20:43:09

hrp 发表于 2020-7-25 17:24
这是语法问题

啊我sb了…感谢!
页: [1]
查看完整版本: with open as f出现SyntaxError