Python FileNotFoundError: [Errno 2] No such file or directory

我只是一个虾纸丫 提交于 2020-01-07 02:13:06

问题


I have the following snippet from a script

credentials = ServiceAccountCredentials.from_json_keyfile_name('tok.json', ['https://www.googleapis.com/auth/analytics.readonly'])

This is for auth in google analytics But when I run the script on my site, the logs report the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'tok.json'

I tried putting tok.json in all places (directories?) I can think of on the server, but the error does not disappear


回答1:


I had this same error. I found out that if I cd into the directory before running the program it runs fine. But obviously, that's not something you'd want to manually do every time. So, I adjusted the file.json to include the absolute path to the file. In other words:

Instead of:

'tok.json'

I changed it to:

'user/folder/tok.json'

I'm not sure if this is the best way to go about it since it will break if you ever move the files to a new directory, but it worked for me.

I think the problem is that when the program runs it's looking in whatever folder the terminal/server is currently in, not necessarily in the program's folder. So you have to explicitly tell it where to go.



来源:https://stackoverflow.com/questions/37669782/python-filenotfounderror-errno-2-no-such-file-or-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!