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.
13 9306 bvdet 2,851
Recognized Expert Moderator Specialist
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: - >>> fullpath = "c:\\program^files\\programb.py"
-
>>> import os
-
>>> os.path.splitdrive("c:\\program^files\\programb.py")
-
('c:', '\\program^files\\programb.py')
-
>>> os.path.split("c:\\program^files\\programb.py")
-
('c:\\program^files', 'programb.py')
-
>>> os.path.join('c:\\program^files', 'newscript.py')
-
'c:\\program^files\\newscript.py'
-
>>>
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\userna me\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...
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.
The way I have done this is to use the sys module, like so: -
import sys
-
path = sys.path[0]
-
print path
-
The current path will be printed out.
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...)?
@scaldo
What are you trying to do with this? I think os.popen is for executing programs, not for opening text files.
@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.
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: -
import sys
-
path = sys.path[0]
-
path will be the current path.
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.
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: 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: 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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |