[Tutor] Question on how to open other programs and files

Alan Gauld alan.gauld at btinternet.com
Sun Feb 8 10:03:07 CET 2009


"Hi" <haztang17 at gmail.com> wrote

>I have a question regarding how to open other programs and files in 
>Python
> code. I am creating a simple interface and I want it to be able to 
> open
> other files, such as a text file or a pdf file.

OK, lets clarify something first.
When you say open do you mean you want to open the file in Python
such that you can read the data into your python program? Or do
you mean you want to open the file in the default application on
your computer. In other words for a pdf file do you want to read
the data in the file or do you want to launch Acrobat on the file?

> usually associated with other programs - for instance, gedit for 
> text and
> document viewer for pdf (I am using Ubuntu 8.10). What syntax or 
> commands do
> I need in order to do so.

The "official" approach to laumnching applications is to
use the subprocess module. This replaces several other
mechanisms which are now considred obsolete.

> I've used os.popen in Windows XP and it works
> fine, but the same code does not work in Ubuntu.

It should work but subprocess replaces popen.

> def openfile(self, event):
>     result =  os.popen("home/user/textfile.txt")
>
> In the terminal it simply says, sh: home/user/textfile.txt: not 
> found.

I suspect your path is wrong. Notice you are not using
an absolute path but a relative path, so unless you have
a directory called home in your current directory it will fail.

But this is not opening the text file this is trying to execute
the text file and that is unlikely to work on Ubuntu. (In Windows
it will launch the default app - probably Notepad) So back to
the original question: do you want to open the t5extfile in
Python or do you want to launch gedit from Python?

Untiol you resolve that conundrum we can't really help
much more.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list