Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a script that is well-tested and known to be working. I am running into an issue with it where when I try to run it from /root/backup/script.py or ~/backup/script.py I get a file not found error from the script when it runs and looks for other files called from within the script. It works on other machines but settings are the same and it works if I am in my home directory and run python backup/script.py or run it directly with python script.py it works. The files it is trying to get are inside the same directory as script.py so I can't see what's wrong. It works to get the first file the script calls but fails to get this one.

I am thinking it could be some kind of path issue as it works on other systems if it is done the same way.

EDIT

after reading the first comment I have posted the code snippet that is falling over and realised that the first file is also not being found like I said it was sorry for the mistake here is the code. As I said above it works from other location but not the two I mentioned.

storage = Storage("user_creds.dat")
credentials = storage.get()
if credentials is None or credentials.invalid:
        credentials = run_flow(flow_from_clientsecrets("client_secrets.json", OAUTH_SCOPE, ), storage, fla$
share|improve this question
    
It may be helpful to post a simplified version of the script that only reproduces the problem; at least post the lines that look for the files that can't be found –  drs May 23 '14 at 22:02
    
Ok I have added code to the question that causes my issue with the two paths. The functions called are taken from the Google API python client –  bobthemac May 23 '14 at 22:12
    
What directory is user_creds.dat in? –  drs May 23 '14 at 22:28
    
The same directory as script.py thats where all files are. –  bobthemac May 23 '14 at 22:36

1 Answer 1

up vote 1 down vote accepted

The file not found error may be due to you launching the script from different directories.

storage = Storage("user_creds.dat")

will only find user_creds.dat if the script is launched from that directory, e.g.,

]$ pwd
~/backup
]$ python script.py

If you need to be able to launch the script from anywhere, you can specify an absolute path to user_creds.dat. Alternatively, you can use the following to get the directory that the script is located:

os.path.dirname(os.path.realpath(__file__))

You can then get your Storage object with

os.path.join(os.path.dirname(os.path.realpath(__file__)), 'user_creds.dat')
share|improve this answer
    
Assuming that the Storage class is an extension of a file object (as I couldn't find it in the standard docs), this being the problem can be checked by printing ` os.path.dirname(os.path.realpath(storage))` post-assignment. Because yay, debugging? –  ChemicalRascal May 24 '14 at 6:39
    
Looks like it was due to different directories don't think the script likes absolute directories as it works fine with relative but fixed now. Thanks. –  bobthemac May 24 '14 at 7:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.