os.system vs. Py2Exe

astyonax astyonax at libero.it
Fri Oct 1 13:29:45 EDT 2004


Lad wrote:

>>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
Try with other version of popen such as popen2, popen3, popen4. I find 
useful popen3, that pause my script.
os.popen3(...)

bye
Astyonax



More information about the Python-list mailing list