Python Forum
os.system doesn't generate same result as shell?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.system doesn't generate same result as shell?
#1
HI, I am a newbie of python and tried to use mixed python and shell code in my python script to achieve my goal. Therefore I use quite a lot of os.system() and/or subprocess.call(). I found that for some commands, the python generate different output from run shell directly. For example:

I have a source text file sample.txt which has lines ended with ":". I'd like to combine this kind of line with the next line. Since I know how to do this easily using vim, like "vim -e -c '%s/:\n/:/' -c 'wq' sample.txt", in my python code, I tried to os.system("vim -e -c '%s/:\n/:/' -c 'wq' sample.txt"), but the substitution did not happen. It looks like the "\n" is not recognized (it works when '%s/:/:ABC/'). Same behavior for subprocess.call("vim -e -c '%s/:\n/:/' -c 'wq' sample.txt", shell=True).

So my question: is os.system(shell_cmd) supposed to generate same result as running the shell_cmd directly? Any way to get around the above problem, yet still use vim instead of new python code?

My environment: python 2.7, bash under linux.

Thanks.
--Karl
Reply


Messages In This Thread
os.system doesn't generate same result as shell? - by abc12346 - Jul-24-2018, 08:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can a program execute code in iPython shell and get result? deanhystad 3 2,811 Jun-17-2022, 03:45 AM
Last Post: Larz60+
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 19,829 Jan-11-2021, 06:30 PM
Last Post: ykumar34
Question Difference between Python's os.system and Perl's system command Agile741 13 9,499 Dec-02-2019, 04:41 PM
Last Post: Agile741
  running python script from shell invoked with os.system("x-terminal-emulator -e /bin/ markhaus 2 4,001 Feb-21-2019, 11:55 PM
Last Post: markhaus
  Why doesn't this work in the Python Shell? diemildefreude 10 13,226 Oct-28-2016, 08:27 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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