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
to open a file for reading: -
-
file = open("c:\path\to\file.txt", 'r') # Windows
-
file = open("/home/user/file.txt", 'r') # for Linux
-
for line in file:
-
print line,
-
file.close()
-
-
This will print out each line of "file".
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.
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? - import os
-
os.system("'C:/myprogram' 'C:/myfile.txt'")
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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...
|
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)
|
by: Chandra |
last post by:
Hi,
Is there a way to execute a python script(file) in ASP.NET application
(programmatically)??
Regards,
Chandra
| |
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
|
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?
...
|
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
|
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>
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |