According to the documentation of os.system
:
In Unix, the return value is the output status of the encoded process in the format specified for wait()
. Note that POSIX does not specify the meaning of the return value of function C system()
, then the return value of the Python function is system dependent.
In Windows, the return value is the one returned by the system shell after running command. The shell is provided by the Windows environment variable COMSPEC
: usually it is cmd.exe
, that returns the output status of the command execution; on systems that use a non-native shell, refer to the shell documentation.
Then use os.system
to obtain the output stdout of a program is not a good idea.
One option is to use the function run
, module subprocess
. Sort of like this:
from subprocess import run, PIPE
def save(filename, info):
with open(filename, 'w', encoding='utf-8') as file:
file.write(info)
result = run('ifconfig', stdout=PIPE)
save('resultado.txt', result.stdout.decode('utf-8'))
Note that we passed the argument stdout
as subprocess.PIPE
so that he may be captured. Moreover, result.stdout
is a bytes Object, so that we use the method decode
to convert it to a UTF-8 string.
Like run
is relatively recent in Python (>= version 3.5), you can use the function check_output
from the same module if you need an older support.
Sensational, worked perfectly, Thank you, Paul!
– Bruno Felipe Kouuds
Just doubt this f" serves for what ?
– Bruno Felipe Kouuds
f-string : example:
print(f"O vavor é {sua_variavel}")
is the equivalent ofprint("O valor é %s" % str(sua_variavel))
. I think f-string came into existence from 3.7 - See more here– Paulo Marques
I didn’t know, now I’ve learned one more rsrs Thank you!
– Bruno Felipe Kouuds