MAC OS - os.system(command) display nothing
Asked Answered
B

2

-2

When I run IDLE (python 3.8) :

>>> import os
>>> os.system("ls")
0
>>> os.system('echo "test"')
0
>>> os.system("users")
0
>>> 

But if i do :

os.system("ls > test.txt") 

The test.txt file is created and contain the result.

I tried to reinstall and everything, nothing works... Only if I Run python in my terminal, command works but IDLE seems better for work.

Who could help me ? Thank you

Bumboat answered 22/11, 2019 at 2:2 Comment(1)
The output of os.system() goes directly to your terminal; Python plays no part in the process. If you aren't running from a terminal, the output has nowhere to go. Use the subprocess module to run programs in a way that lets you do something with their output.Grados
P
0

I get the same issue and this is what I did:

  • Restart the IDLE.
  • Restart my laptop.

However, the same problem persists.

My suggestion is, instead of using IDLE, why don't you use the terminal? In my case, I use iTerm.

$ python3.8
$ >>> import os
$ >>> os.system("ls")

And everything works fine.

Penetrate answered 22/11, 2019 at 2:30 Comment(0)
H
-1

If you start IDLE from Terminal with `$ python3 -m idlelib' them output from os.system will appear in Terminal while the return value is printed in the IDLE Shell. But I unless I were developing a Python script that uses os.system, I would just keep IDLE and Terminal both open and type the appropriate commands in each.

Hass answered 22/11, 2019 at 6:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.