Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using a Ubuntu command in replit (os.system)

I am trying to use the command

cmd = "grep -n 'str' file.txt"

in the script, im trying to use

command = os.system(cmd)

but when i try to print the variable, it only prints a '0', but in the output appears 1:str. Is there a way to make set this output as a variable?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re getting 0 because that’s the exit code of the process. Per the documentation for os.output():

On Unix, the return value is the exit status of the process

To get the behavior you want, use the subprocess package instead, like this:

import subprocess
command = subprocess.check_output(cmd, shell=True)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading