0
def openFileReadRobust():

source = None
while not source:                  # still no successfully opened file
  filename = input('What is the filename? ')
  try:
    source = open(filename)
  except IOError:
    print ('Sorry. Unable to open file',filename)
  return source        



lineList=openFileReadRobust().readlines()
print(lineList)

当我在 python 编辑器中尝试这个时,
当我输入一个有效的文件名时它会打印我的列表

Enter name of file: file.txt

 ['a1,2,3,4\n', 'b1,2,3,4\n']

但是当我使用终端时,它会让我在文件名周围加上引号。例如,如果我在终端中输入 file.txt 它会说

NameError: name 'file' is not defined

没有 .txt 扩展名

有没有办法让终端接受不带引号的 file.txt?

4

0 回答 0