473,796 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to execute a program in python script directory

6 New Member
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:\pr ogram^files\pro gramb.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
13 9308
pythonner
11 New Member
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 New Member
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
11 New Member
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 Recognized Expert Contributor
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
5327
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 these files, I wish to do the same using another python code instead of running each of these files one by one, which would be cumbersome giving the argv of each file every single time. This can be easily done using a shell script but I just...
2
2597
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 python script? For example, I have a script.py under "/home/john/". Can I just type "python script.py" to run it under another directory "/home/tom/"? Thanks! Yang
4
3053
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 dns records at my dns provider, zoneedit. So basically, I've setup dynamic dns using python. Once the ip compare and/or update is complete, I log the results to a text file called update.log. When I run the program in a bash shell with the...
9
19129
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 the space in the path to the exe is being handled correctly and that the program was executed. CMD= r'"C:\program files\some directory\engine\theexe.exe"' os.system(CMD)
5
7539
by: Chandra | last post by:
Hi, Is there a way to execute a python script(file) in ASP.NET application (programmatically)?? Regards, Chandra
19
11386
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 in the script. I know I can use os.system() or os.popen() to run an external file, but these functions take as argument a string evaluated as command-line. I also know I could save the binary code as a temporary file, execute
7
10007
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. Essentially, I would like to password protect an entire directory of files. Is there a way to auto execute a python script after a user double clicks to open a folder on the USB drive? How can you capture that double click event on a specific folder? ...
4
2361
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 and selecting 'open with python', the execution occurs in my home directory, not my desktop. Is there a way to force py scripts to always run within the directory that they reside in? Thanks
3
4944
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 "D:/softwares/Apache2.2/htdocs"> AddHandler mod_python .py PythonHandler mptest PythonDebug On </Directory>
0
9529
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.