问题
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