Python, No such file or directory:***

Q:
Python中使用某文件,引用时使用相对路径(同级),出现No such file or directory,但是文件存在,使用完整的绝对路径是正常的。

configs = json.load(open(‘config.json’, ‘r’))
FileNotFoundError: [Errno 2] No such file or directory: ‘config.json’

A:
解决办法是使用在使用路径前,添加一句os.chdir(dir), dir是对应的完整的绝对路径
os.chdir(r’[path]')
configs = json.load(open(‘config.json’, ‘r’))

  • Call os.chdir(dir), dir being the folder where the file is located, then open the file with just its name like you were doing.
  • Specify an absolute path to the file in your open call.

(我用相对路径也行,虚拟环境的下一级开始,例如,虚拟环境为D:a/b/c/d,在b下新建了一个python虚拟环境,我在dir处填写c/d)

https://stackoverflow.com/questions/12201928/open-gives-filenotfounderror-ioerror-errno-2-no-such-file-or-directory

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值