By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

How to execute a program in python script directory

P: 6
I am just starting out on Python and trying to make a simple interface as practice. Right now I can execute a program through the os.popen command. However, I have to specify the directory where the program is located. For example, say:

os.popen("c:\program^files\programb.py")

Is there any way I can tell Python to assume the default directory is wherever the interface script is ran from? So say I have a folder and inside are all my python files. So when I click on a button to open programb, the script knows to only search in that folder. I am trying to make everything self-contained so the folder may be in different places in the directory (could be C:\, desktop, c:\program files, etc.). By the way, Python is an AWESOME language, I can't believe I didn't get into this earlier in my life.
Feb 4 '09 #1
Share this Question
Share on Google+
13 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Since you know the path to the original script, you can use the os module to split the directory from the file name, and join the directory to any other file name. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> fullpath = "c:\\program^files\\programb.py"
  2. >>> import os
  3. >>> os.path.splitdrive("c:\\program^files\\programb.py")
  4. ('c:', '\\program^files\\programb.py')
  5. >>> os.path.split("c:\\program^files\\programb.py")
  6. ('c:\\program^files', 'programb.py')
  7. >>> os.path.join('c:\\program^files', 'newscript.py')
  8. 'c:\\program^files\\newscript.py'
  9. >>> 
Feb 5 '09 #2

P: 6
Thank you very much for the reply bvdet! I think even though you misunderstood me, you still managed to answer half of my question! What I am asking is my main interface python file and any other python scripts I want to call are all in the same folder. The issue is the folder may not always be in program files; it could be on desktop (which could be something like c:\documents and settings\username\desktop), it could be just in c:\, etc. So the directory may change. Is there any way to parse the main interface file location and then use the split technique you showed above to append it to my other python scripts that I might want to call from the main interface file.

The reason I'm asking this is since I'm going to keep all the python scripts I want in one folder, I might migrate to another desktop environment such as ubuntu, which the file directory wouldn't be the same, or I could move the folder from desktop on my C drive to D drive, etc. Sorry for being misleading; it was difficult for me to frame the question too...
Feb 5 '09 #3

kaarthikeyapreyan
100+
P: 107
So when I click on a button to open programb, the script knows to only search in that folder. I am trying to make everything self-contained so the folder may be in different places in the directory (could be C:\, desktop, c:\program files, etc.)
The path for the file that is going to run has to be either hardcoded or must be obtained from the runtime from a user or process, either of the ways you can get where the file is working from, then you could very well use the os.chdir(<PATH obtained during runtime>) and execute the other scripts or set the PATH to a variable and access it later.
Feb 5 '09 #4

pythonner
P: 11
The way I have done this is to use the sys module, like so:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2. path = sys.path[0]
  3. print path
  4.  
The current path will be printed out.
Feb 5 '09 #5

P: 6
Thanks everyone! I will try and see what I can do. However, I have encountered a second problem. The os.popen command with the file location given does not seem to work at all in Ubuntu. For instance, if I just have

os.popen('/home/scaldo/Desktop/somefile.txt')

does not work. Even if I use chmod +x somefile.txt and I know the file location is correct.I am new at Ubuntu, and the Linux world in general. Is there something I am missing (I feel I am...)?
Feb 7 '09 #6

pythonner
P: 11
@scaldo
What are you trying to do with this? I think os.popen is for executing programs, not for opening text files.
Feb 8 '09 #7

P: 6
@pythonner
I'm just testing waters with the features in Python. So the program will be able to open other programs as well as open files (such as text files). I was looking into this, and the other one to my knowledge would be the os.system, but neither one worked. Basically I just want to know the commands to open other programs as well as files. I've tried looking online but could not find one that would work. It works in Windows, but Ubuntu is a different beast altogether, so having a hard time porting over my code in Windows into Ubuntu.
Feb 8 '09 #8

pythonner
P: 11
I have been dealing with the same issue. I'm also a python noob, but a couple of things comes to mind: remember, Windows uses a backward slash, '\', and linux uses a forward slash, '/'.

Also, to have your python script use the current path, use:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2. path = sys.path[0]
  3.  
path will be the current path.
Feb 8 '09 #9

P: 6
Thanks for the reply again. Your method does indeed work for my situation; however, I don't know how to implement it. Like I've said above, I really only know the system and popen commands to open/execute a program or file. How would I use the correct syntax to make this work? Like what I have now would be result = os.system(path/somefile.txt), but that doesn't work. Sorry for the newbieness... just having a hard time grasping all the finer details of Python.
Feb 8 '09 #10

pythonner
P: 11
to open a file for reading:

Expand|Select|Wrap|Line Numbers
  1.  
  2. file = open("c:\path\to\file.txt", 'r')  # Windows
  3. file = open("/home/user/file.txt", 'r')  # for Linux
  4. for line in file:
  5.     print line,
  6. file.close()
  7.  
  8.  

This will print out each line of "file".
Feb 8 '09 #11

P: 6
Thanks again. Is there any way I can open the file using another program, but it's called by the Python script? For example, if I want to use my Python script to open a text file using gedit or a pdf file using document viewer. Because ultimately I want to leave the terminal and have as much "GUI" as possible. Is there a generic way to open a file that may be handled by another program (such as, again, gedit for text or document viewer for pdf). Nevertheless, I really do appreciate the help, it's been a tremendous boost to what I would otherwise be in a big headache.
Feb 8 '09 #12

pythonner
P: 11
Sorry, I haven't crossed that bridge yet. Try asking on the python tutor mailing list.

http://mail.python.org/mailman/listinfo/tutor
Feb 8 '09 #13

boxfish
Expert 100+
P: 469
I'm not sure I completely understand your question, but would it work to use an operating system specific command through the os.system function?
Expand|Select|Wrap|Line Numbers
  1. import os
  2. os.system("'C:/myprogram' 'C:/myfile.txt'")
Feb 8 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.