用Python打开文件的正确方法是什么?

时间:2019-04-17 18:53:46

标签: python-3.x visual-studio-code

我在Mac上使用VSCode,我不知道为什么会得到

FileNotFoundError: [Errno 2] No such file or directory: 'flying_circus_cast.txt' 

,带有以下代码段。文本文件与python文件位于同一目录中。

我也尝试过./flying_circus_cast.txt,但遇到相同的错误

def create_cast_list(filename):
    cast_list = []
    #use with to open the file filename
    #use the for loop syntax to process each line
    #and add the actor name to cast_list
    with open('flying_circus_cast.txt', 'r') as f:
        print(f.read())

    return cast_list

cast_list = create_cast_list('flying_circus_cast.txt')
for actor in cast_list:
    print(actor)

0 个答案:

没有答案