473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to execute a program in python script directory

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
13 9270
bvdet
2,851 Expert Mod 2GB
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
scaldo
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
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
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
scaldo
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
@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
scaldo
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
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
scaldo
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
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
scaldo
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
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
469 Expert 256MB
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

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

Similar topics

5
by: Shalen chhabra | last post by:
Hey, Can anyone give me a snippet for running a python program over all the files in the directory. For ex: I have ten files in a directory and I want to run a python program against all of...
2
by: myang | last post by:
After we compile a C program, we can put the executable file under a directory (like /bin) so that we can run it everywhere, and don't have to type the full path. Can we do the same thing to a...
4
by: vagrantbrad | last post by:
I'm using python 2.4 running on Fedora Core 4. I have written a python program called ipscan.py that checks the external ip address of my cable internet connection, and on change, will update the...
9
by: sdb1031 | last post by:
I am trying to run an exe within a python script, but I'm having trouble with spaces in the directory name. The following example will display the usage statement of the program, so I know that...
5
by: Chandra | last post by:
Hi, Is there a way to execute a python script(file) in ASP.NET application (programmatically)?? Regards, Chandra
19
by: citronelu | last post by:
Is it possible to execute a binary string stored within a python script as executable code ? The script is run under Windows, and the binary code (a full executable file) is stored in a variable...
7
by: Brian Erhard | last post by:
I am still fairly new to python and wanted to attempt a home made password protection program. There are files that I carry on a USB flash drive that I would like to password protect. ...
4
by: brad | last post by:
When I use idle or a shell to execute a python script, the script executes in the directory it is currently in (in this case, my desktop). However, when using GNOME and right clicking the py script...
3
by: joe jacob | last post by:
I configured apache to execute python scripts using mod_python handler. I followed below mentioned steps to configure apache. 1. In http.conf I added <Directory...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.