VScode+Python找不到同目录下的文件

打开Settings,搜索python.Terminal,勾选Python>Terminal:Execute In File Dir,

设置文件目录为工作目录.

try:    
    f = open("poetry.ini", 'rb')
    data = f.read(3)

    print(str(data, 'utf-8'))
except OSError as e:
    print(e)
    
finally:
     if 'f' in globals():#判断f是全局变量,不加这句,在VSCode中会提示f不存在,但是IDLE中没有报错
        print('close the file')
        f.close()

 

你可能感兴趣的:(python)