Shell – How to change the working directory for a shell script

cd-commandpythonshell

I have a Python script that looks files up in a relative directory. For example: the Python script is in /home/username/projectname/. I have a file that is being called within the Python script that is in /home/username/projectname/subfolder.

If I run the script from the shell as python scriptname.py it runs perfectly fine.

However, I'm trying to run the script as a startup service. I'm setting it up in webmin, and I believe its creating a shell script to call it. Through the startup script, I'm doing something like this to call the script:

execute python home/username/projectname/scriptname.py

The script is starting up fine, but it can't access the files in the relative directory.

I am guessing that there is a better way to call the Python program from within the startup script so that its aware of the relative path.

Best Answer

Quick and dirty:

In your start up script instead of just executing the python script, use cd first.

#!/bin/sh

cd /home/username/projectname &&
python ./scriptname.py
Related Question