os.system vs. Py2Exe

Lad export at hope.cz
Fri Oct 1 12:53:30 CEST 2004


> You can use this instead of os.system()
> 
> import os
> fh=os.popen("notepad 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.

Thanks a lot for your help.
os.popen can open file on both Xp and Win98. But there is still a
problem.
When I open myfile.txt (on Win98) by
os.popen("notepad myfile.txt")
my Python program continues running but I would need it to wait until
notepad
window ( with myfile.txt) is closed.
On XP platform the python program execution is paused until I close
the Notepad window.The same I need on Win98 platform
Thanks for help.
LAd



More information about the Python-list mailing list