os.system vs. Py2Exe

L

Lad

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,

os.system

causes the following error:
Bad file or command name

Any help would be appreciated.
Lad
 
T

Thomas Guettler

Am Tue, 28 Sep 2004 03:00:23 -0700 schrieb Lad:
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,

os.system

causes the following error:
Bad file or command name

Hi,

What do you execute with os.system?

If the script you execute startswith
#!INTERPRETER
you might have trouble. I don't think this
is well supported under windows.

Try this: os.system("INTERPRETER SCRIPT")


Regards,
Thomas
 
G

Greg

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,

os.system

causes the following error:
Bad file or command name

Any help would be appreciated.
Lad

Did you install the python dll as well?
 
L

Lad

Did you install the python dll as well?

Yes, I installed all files( I think) because everything else works.
Only os.system does NOT.
I use os.system to open txt file.I use it like os.system('Myfile.txt')

When I use os.startfile instead of os.system it works well.
 
G

Grant Edwards

Yes, I installed all files( I think) because everything else
works. Only os.system does NOT. I use os.system to open txt
file.I use it like os.system('Myfile.txt')

What makes you think that should work?

Is Myfile.txt an executable file? Under Win32, don't
executble file's names usually end in .exe?
When I use os.startfile instead of os.system it works well.

Then why not use os.startfile()? It's the correct way to start
the program that is supposed to handle .txt files.
 
P

Peter Hansen

Lad said:
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?
 
J

Jeff Shannon

Lad said:
Yes, I installed all files( I think) because everything else works.
Only os.system does NOT.
I use os.system to open txt file.I use it like os.system('Myfile.txt')

When I use os.startfile instead of os.system it works well.

I suspect that the situation here is that you've got some unusual WinXP
setting that's telling it that .txt files are executable in some way.
If os.system('file.txt') works on your system, then it's your system
that's the odd one; Python (and the other computer) are behaving as
expected. (It's possible, in Win2K/WinXP, to register particular
extensions as 'executable' through a specific other program; this is how
..py scripts are handled, so typing 'script.py' is equivalent to typing
'python script.py'.)

Remember, os.system() is roughly equivalent to typing the argument at a
command line. Does typing "myfile.txt" at a commandline prompt open
that file in a text editor? (Hint -- it shouldn't, under normal
circumstances.)

In this case, I'm pretty sure that using os.startfile() is the
appropriate way of doing what you want.

Jeff Shannon
Technician/Programmer
Credit International
 
P

Peter Hansen

Jeff said:
I suspect that the situation here is that you've got some unusual WinXP
setting that's telling it that .txt files are executable in some way.
If os.system('file.txt') works on your system, then it's your system
that's the odd one; Python (and the other computer) are behaving as
expected. (It's possible, in Win2K/WinXP, to register particular
extensions as 'executable' through a specific other program; this is how
.py scripts are handled, so typing 'script.py' is equivalent to typing
'python script.py'.)

Remember, os.system() is roughly equivalent to typing the argument at a
command line. Does typing "myfile.txt" at a commandline prompt open
that file in a text editor? (Hint -- it shouldn't, under normal
circumstances.)

Unfortunately for your theory, on XP typing just the name of the
file (at least with a .txt file) does indeed open it in the associated
application.

I suspect the "other" machine he is using is not an XP machine, and
thus not suffering from such unexpected behaviour. Thanks, Mickysoft...

-Peter
 
J

Jeff Shannon

Peter said:
Unfortunately for your theory, on XP typing just the name of the
file (at least with a .txt file) does indeed open it in the associated
application.


Why, so it does... and under Win2K, as well.
I suspect the "other" machine he is using is not an XP machine, and
thus not suffering from such unexpected behaviour. Thanks, Mickysoft...


Yes, if the "other" machine is Win9x/ME (or, I believe, NT4), using the
..txt filename as a command will result in an error. As it should, also,
under *nix (and probably MacOS) as well, unless I'm even more confused
than I'd thought. ;)

This does, at least, do away with the need to presume some sort of
screwy settings on the O.P.'s machine -- said screwy settings being
provided by default from Microsoft. ;)

Jeff Shannon
Technician/Programmer
Credit International
 
B

Bengt Richter

What makes you think that should work?

Is Myfile.txt an executable file? Under Win32, don't
executble file's names usually end in .exe?

It's not a matter of _being_ executable, it's a matter of having an executable
_associated with_ the extension. Typically notepad.exe is associated with .txt
(via an intermediate association with a generic file type name that in turn
is associated with the actual executable):
----
[16:30] C:\pywk\junk>assoc .txt
..txt=txtfile

[16:30] C:\pywk\junk>ftype txtfile
txtfile=C:\WINNT\System32\NOTEPAD.EXE %1
----
Then why not use os.startfile()? It's the correct way to start
the program that is supposed to handle .txt files.
One difference is that os.system will wait for the completion of what it starts,
but os.startfile will start a new process to do it, and return immediately.

I would try it with full absolute paths to everything the in the os.system call.
If that works, you can look for why the environment is different.

BTW, if you execute via start menu or double clicking a desktop icon, the associated
..lnk may specify the starting directory. If that is different from current directory,
bare names may be looked for in unexpected place and not be found (or worse, a file
of the same name may be found, but be the wrong one). Right click on the icon and look
at the properties. Or right click the task bar and chase properties of start menus that way,
or start>settings>taskbar> etc.

BTW2, if you want to type in a file name without extension, as you can with .exe
and .bat etc., it implies a priority choice if e.g. both .exe and .bat exist
in the same path directory. You can influence this by the PATHEXT environment
variable. Go to a console window and type
help ftype|more
and also
help assoc|more
for additional info

BTW3, beware of changing .exe association!! I was aghast to find (some time ago) that
NS4.5 let me change that association via its file association option interface.
It was pretty squirrely to get back to normal. No console commands would work.
(IIRC I finally used start menu>run>browse to find and execute regedit32 and repaired
it that way, after booting in last known good to see what the registry was supposed to be
for .exe). Very anxious moments there ;-)

Regards,
Bengt Richter
 
L

Lad

What makes you think that should work?

Is Myfile.txt an executable file? Under Win32, don't
executble file's names usually end in .exe?

It's not a matter of _being_ executable, it's a matter of having an executable
_associated with_ the extension. Typically notepad.exe is associated with .txt
(via an intermediate association with a generic file type name that in turn
is associated with the actual executable):
----
[16:30] C:\pywk\junk>assoc .txt
.txt=txtfile

[16:30] C:\pywk\junk>ftype txtfile
txtfile=C:\WINNT\System32\NOTEPAD.EXE %1
----
Then why not use os.startfile()? It's the correct way to start
the program that is supposed to handle .txt files.
One difference is that os.system will wait for the completion of what it starts,
but os.startfile will start a new process to do it, and return immediately.

I would try it with full absolute paths to everything the in the os.system call.
If that works, you can look for why the environment is different.

BTW, if you execute via start menu or double clicking a desktop icon, the associated
.lnk may specify the starting directory. If that is different from current directory,
bare names may be looked for in unexpected place and not be found (or worse, a file
of the same name may be found, but be the wrong one). Right click on the icon and look
at the properties. Or right click the task bar and chase properties of start menus that way,
or start>settings>taskbar> etc.

BTW2, if you want to type in a file name without extension, as you can with .exe
and .bat etc., it implies a priority choice if e.g. both .exe and .bat exist
in the same path directory. You can influence this by the PATHEXT environment
variable. Go to a console window and type
help ftype|more
and also
help assoc|more
for additional info

BTW3, beware of changing .exe association!! I was aghast to find (some time ago) that
NS4.5 let me change that association via its file association option interface.
It was pretty squirrely to get back to normal. No console commands would work.
(IIRC I finally used start menu>run>browse to find and execute regedit32 and repaired
it that way, after booting in last known good to see what the registry was supposed to be
for .exe). Very anxious moments there ;-)

Regards,
Bengt Richter

..txt file is associated with a propper application( Notepad.txt). AS I
said above, with os.startfile the file is open in Notepad.txt but not
if I use os.system. I can not use os.startfile, because it does not
wait until .txt file is closed
 
P

Peter L Hansen

Lad said:
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...

-Peter
 
L

Lad

Peter L Hansen said:
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')
 
B

Bengt Richter

On 28 Sep 2004 23:46:34 -0700, (e-mail address removed) (Lad) wrote:
[...]
.txt file is associated with a propper application( Notepad.txt). AS I
said above, with os.startfile the file is open in Notepad.txt but not
if I use os.system. I can not use os.startfile, because it does not
wait until .txt file is closed
Just curious, what happened when you tried specifying the full absolute path to your
..txt file when calling os.system? e.g.
filename = 'something.txt' # for example
os.system(os.path.abspath(filename))
?

Regards,
Bengt Richter
 
T

Tony C

Peter L Hansen said:
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.
 
L

Lad

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
 
A

astyonax

Lad said:
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
 
M

Michael P. Dubner

Lad said:
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,

os.system

causes the following error:
Bad file or command name

Any help would be appreciated.
Lad
Check if
os.system('start /wait File.txt')
is working under Win9x.
It's for sure will work under WinNT+
 
L

Lad

Michael P. Dubner said:
Check if
os.system('start /wait File.txt')
is working under Win9x.
It's for sure will work under WinNT+

os.system('start /wait File.txt') WORKS!! Thanks a lot
Lad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top