Python script run via cron job returning IOError [error no2]
Question: I'm running a Python feedparser script via cron job on a Centos6 remote server
(SSHing into the server).

In Crontab, this is my cron job:

    
    
    MAILTO = myemail@company.com
    */10 * * * * /home/local/COMPANY/malvin/SilverChalice_CampusInsiders/SilverChalice_CampusInsiders.py > /home/local/COMPANY/malvin/SilverChalice_CampusInsiders`date +\%Y-\%m-\%d-\%H:\%M:\%S`-cron.log | mailx -s "Feedparser Output" myemail@company.com
    

However, I'm seeing this message in the email that's being sent, which should
just contain the output of the script:

    
    
    Null message body; hope that's ok
    /usr/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
      InsecurePlatformWarning
    Traceback (most recent call last):
      File "/home/local/COMPANY/malvin/SilverChalice_CampusInsiders/SilverChalice_CampusInsiders.py", line 70, in <module>
        BC_01.createAndIngest(name, vUrl, tags, desc)
      File "/home/local/COMPANY/malvin/SilverChalice_CampusInsiders/BC_01.py", line 69, in createAndIngest
        creds = loadSecret()
      File "/home/local/COMPANY/malvin/SilverChalice_CampusInsiders/BC_01.py", line 17, in loadSecret
        credsFile=open('brightcove_oauth.json')
    IOError: [Errno 2] No such file or directory: 'brightcove_oauth.json'
    

Normally, this would be a no-brainer issue: something must be wrong with my
code. Except, the script works perfectly fine when I run it on the command
line via `python SilverChalice_CampusInsiders.py`

What am I doing wrong here? Why doesn't the Python script "see" the json oauth
file when run via cron job?


Answer: Cron sets a minimal environment for the jobs (and I think it runs the job from
the home directory).

Inside your python script, when you do something like -

    
    
    open('<filename>')
    

It checks for the `filename` in the current working directory , not the
directory in which your scripts exist.

That is true even when running from commandline , if you change directory to
some other directory (maybe your home directory) , and then use absolute path
to your script to run it, you should be getting the same error.

Instead of depending on the current working directory to be correct and have
the file you want to open, you can try either of the below options -

  1. Use an absolute paths to the files you want to open , do not use relative path.

  2. Or If the above is not an option for you, and the files you want to open are present relative to the script that is getting run (for example purpose lets say in the same directory) , then you can use `__file__` (this gives the script location) and [`os.path`](https://docs.python.org/2/library/os.path.html) , to create the absolute path to your file at runtime, Example -
    
        import os.path
    
    fdir = os.path.abspath(os.path.dirname(__file__)) #This would give the absolute path to the directory in which your script exists.
    f = os.path.join(fdir,'<yourfile')
    

At the end `f` would have the path to your file and you can use that to open
your file.

