os.system vs. Py2Exe

Tony C cappy2112 at gmail.com
Wed Sep 29 20:19:18 EDT 2004


export at hope.cz (Lad) wrote in message news:<81a41dd.0409290652.61d6aaa4 at posting.google.com>...
> Peter L Hansen <peter at engcorp.com> wrote in message news:<4_CdnUGCt9u3CcfcRVn-pA at powergate.ca>...
> > Lad wrote:
> > > Peter Hansen <peter at engcorp.com> wrote in message news:<Rd2dneVBGfQrXMTcRVn-jQ at powergate.ca>...
> > > 
> > >>Lad wrote:
> > >>
> > >>>I used Py2exe to compile my script( I use XP).
> > >>>The compiled script works OK on my XP where Python is installed.
> > >>>But when I install the compiled exe to another computer,
> > >>
> > >>What operating system is the other computer running?
> > > 
> > > The other Operating system( that does not work with os.system) is Windows 98
> > 
> > That's the source of your problem.  Win98 doesn't work the way
> > XP/NT does, and you *cannot* use os.system() in this way to
> > get the expected behaviour.  Find another approach... maybe
> > read the registry entries for .TXT files and launch NotePad
> > directly...
> I found out the following:
> on Xp I can use
> os.system('IDPass.txt')
> 
> but on Win98
> it does not work.
>  So I must use
> os.system('notepad.exe IDPass.txt') to open IDPass.txt file
> but the problem with Win 98 is the following:
> Notepad is launched but the Python running  program ( exe compiled
> script) is not paused( until I close the Notepad window) but continues
> running with the next command.In other words Notepad runs
> independently on my exe program.
> WHY?
> 
> Lad
> 
> 
>         if verseOS[3]==2:#XP
>             os.system('IDPass.txt')
>         if verseOS[3]==1:#Windows 95/98/ME
>             print "Windows 95/98/ME running"
>             os.system('notepad.exe IDPass.txt')

You can use this instead of os.system()

import os
fh=os.popen("notepade myfile.txt")
fh.close()
(but you should add the appropriate exception handling)

This opens notepad and python will exit if you ran it like this
python myscript.py myfile.txt

If you ran python interactively, then you are returned to the python
command line, after the os.popen() statement. In which case you should
manually close the file, as shown above.

fh is a file object, so if you want to you can read from that object.

This should work on W98, Win2K, Win XP and does not depend on any file
associations.

I would not use os.startfile() for anything, if you are expecting to
open a specific application that is associated with a file extension,
because many applications re-associate file extensions during
installation. Additionally, The user may have intentionally
re-associated .TXT with a different program, because notepad is so
lame.

There are many variations of popen(), but popen() is probably the
simplest for your situation.



More information about the Python-list mailing list