Python Forum
Difference between Python's os.system and Perl's system command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difference between Python's os.system and Perl's system command
#1
Question 
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
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
https://docs.python.org/2/library/subpro...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
Reply
#4
(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)
Reply
#5
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)
Reply
#6
Not required
Reply
#7
If you run the command in a terminal, what is the exact command that works in the terminal (cmd window)?
Reply
#8
Not required
Reply
#9
It is meaningless to try things randomly. Find the command that works in a Cmd window first, outside of any scripting language.
Reply
#10
Not required
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  why I can't pip install faiss-cpu using python 3.14 on my windows 10 system? Cauchy 1 5,489 Oct-17-2025, 06:40 PM
Last Post: snippsat
  How to stop system tray Icons from grouping together phpjunkie 0 1,124 Apr-07-2025, 05:49 AM
Last Post: phpjunkie
  Accessing item in the system tray on Windows 11 mariox 0 1,112 Aug-22-2024, 05:11 PM
Last Post: mariox
  System showing np.int64(xxx) as output leea2024 3 12,943 Aug-10-2024, 10:55 AM
Last Post: leea2024
  how to find difference between fake driving license or real using python? pjaymn 5 6,165 Jun-14-2024, 07:01 AM
Last Post: Pedroski55
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 3,973 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  What is the difference between Command Prompt and Sublimes yaoyao22 1 1,692 Jul-09-2023, 02:56 PM
Last Post: snippsat
  Os.system("shutdown"); command not found cosmin1805 4 4,606 Nov-13-2022, 02:07 PM
Last Post: cosmin1805
  [split] Please help with menu system dvejsa 4 3,145 Sep-13-2022, 08:01 PM
Last Post: dvejsa
  Alarm system with state, class, enum. Frankduc 0 2,424 May-04-2022, 01:26 PM
Last Post: Frankduc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020