0

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 using a terminal command to call it. In the startup command, I'm doing something like this to call the script:

execute python home/username/projectname/scriptname.py. The script is starting up fine, but I get an error because it cant 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 command so that its aware of the relative path.

3
  • What do you mean by relative directory? Relative to the scriptname.py or relative to the execution environment? Commented Jul 9, 2012 at 0:16
  • relative to the scriptname.py... so I need to access the subfolder directory from the script Commented Jul 9, 2012 at 0:18
  • See the last part of my answer, use the __file__ variable, along with other methods to get what you need. Commented Jul 9, 2012 at 0:19

3 Answers 3

3
import os

os.chdir("/tmp")

Also have:

os.getcwd()

Maybe you need the following instead?

import os.path

dir = os.path.dirname(__file__)
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the help. While this works in the script itself, the script is calling other (non python) files within the directory, and those files are having issues as well. So, I think I need to set the directory when I call the file thorough linux commands rather than within python.
What shell is used by the 'execute' command above? Can you set the environment of the shell?
Its bash.... to be honest, I dont know how to set the environment, i'm a linux beginner.
No problem, everyone starts somewhere. In the bash shell you set the current working directory with cd <path>. I don't know webmin, so I don't know how to work that into your execute ... line above.
well, if I run the script just like this: "python home/username/projectname/scriptname.py", then I have the same issue (so its not really webmin). Any idea how I would I run the script in one bash line to get it to set the environment?
|
1

The process that starts your python script (probably forkink) has a pwd (its working directory). The idea is to change the pwd of the process before to fork and execute python.

You need to look over the manual of the process that executes the shell command, and see how to set the pwd.(in shell you use cd or pushd)

Comments

1

__file__ is the path that was used to run your script.

Copy this into a script file and try running it:

import os

print "This script was run as: %r" % __file__
print "This script is: %r" % os.path.abspath(__file__)

script_dir = os.path.dirname(os.path.abspath(__file__))
print "This script is in directory: %r" % script_dir

print "When started, the current directory was: %r" % os.getcwd()

os.chdir(script_dir)
print "After calling os.chdir(), the current directory is: %r" % os.getcwd()

1 Comment

Thanks for the help, I made this same comment above: While this works in the script itself, the script is calling other (non python) files within the directory, and those files are having issues as well. So, I think I need to set the directory when I call the file thorough linux commands rather than within python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.