Python Forum
Difference between Python's os.system and Perl's system command - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Difference between Python's os.system and Perl's system command (/thread-22811.html)

Pages: 1 2


Difference between Python's os.system and Perl's system command - Agile741 - Nov-27-2019

On Windows 10, I am converting a perl script to python, but I have a problem.

The command in the perl script file is:

Quote:system ("a dot exe file with 3 options");

In Python, I use the exact same argument in the following system call:

os.system ("a dot exe file with 3 options")
Note: The two above commands have the exact same string!

My problem is that the executable program is supposed to create 4 files in C:\Temp. The perl script does create all 4 files as it is supposed too. But, the Python script creates only 1 file then gives me a popup window saying, "COULD NOT OPEN THIS C:\TEMP". The program then deletes the file it created.

I know nothing about the executable. It was created my someone years ago.

I tried changing one property of the executable file, so that all users execute it as an administrator. But, that didn't solve the problem.

I tried making all the files involved to be readable but that also didn't work.

Is there a difference between the Perl "system(...)" command and the Python "os.system(...)" command that I should be aware of?

Should I use a different command instead of "os.system()"?

Thank you,
Mike


RE: Difference between Python's os.system and Perl's system command - buran - Nov-27-2019

if your string has c:\temp then \t is escape sequence (i.e. TAB). use forward slash, raw string or escape the backslash.

Also subprocess.run() is recommended over os.system


RE: Difference between Python's os.system and Perl's system command - JRHeisey - Nov-27-2019

https://docs.python.org/2/library/subprocess.html#module-subprocess

It seems that subprocess is closer to the Perl system command.
The Python os.system() function invokes the system shell to launch the command.

Perl
https://perldoc.perl.org/functions/system.html
system() takes a list of strings, each element after the first is a command parameter

Python
https://docs.python.org/2/library/os.html#os.system
os.system() takes a single string
subprocess.call(["ls", "-l"]) optionally takes a list of strings.
subprocess.call("exit 1", shell=True) or a single string


RE: Difference between Python's os.system and Perl's system command - Agile741 - Nov-28-2019

(Nov-27-2019, 09:07 PM)buran Wrote: if your string has c:\temp then \t is escape sequence (i.e. TAB). use forward slash, raw string or escape the backslash.

Also subprocess.run() is recommended over os.system

  1. Are there any problems with me first trying to use a variable with the below code?
  2. How would you modify/improve this specific code?
  3. Do I have alternatives?
  4. Should I try to replace each single backslash with two backslashes? How can I do this?

the_command = "a dot exe file with 3 options"
subprocess.call (r'the_command, shell = True)



RE: Difference between Python's os.system and Perl's system command - Gribouillis - Nov-28-2019

r' is not some magical operator that would apply to abitrary expressions. Instead, r'...' is a special kind of literal string where the \ character is taken literaly instead of being used as an escape character. Thus you can write
the_command = r"a dot exe file with 3 options"
subprocess.call (the_command, shell = True)
Most of the time however, shell=True is superfluous and/or detrimental and it is better to split the command in a list of arguments
import shlex
the_command = shlex.split(r"a dot exe file with 3 options")
subprocess.call (the_command)



RE: Difference between Python's os.system and Perl's system command - Agile741 - Dec-02-2019

Not required


RE: Difference between Python's os.system and Perl's system command - Gribouillis - Dec-02-2019

If you run the command in a terminal, what is the exact command that works in the terminal (cmd window)?


RE: Difference between Python's os.system and Perl's system command - Agile741 - Dec-02-2019

Not required


RE: Difference between Python's os.system and Perl's system command - Gribouillis - Dec-02-2019

It is meaningless to try things randomly. Find the command that works in a Cmd window first, outside of any scripting language.


RE: Difference between Python's os.system and Perl's system command - Agile741 - Dec-02-2019

Not required