how can use cmd in py file? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how can use cmd in py file?

###

21st Dec 2016, 4:08 AM
sam-dev
sam-dev - avatar
2 Answers
+ 1
I use this kind of code instead: import subprocess try: popen = subprocess("echo Hello world!", shell=True, stdout=subprocess.PIPE) ret = popen.wait() except: sys.exit(1) out = popen.stdout.read() print("----- ret: "+str(ret)) print("\n\n----- out:\n"+out) sys.exit(0) ret is an integer with the return value of the command, while you can read the standard output ( stdout ) result as string ( then you can use it for retrieving some information ). I don't know if it work too on ms windows. But by using command line, you break the platform independance from your code ( and mine is on linux ^^ )...
21st Dec 2016, 8:29 AM
visph
visph - avatar
0
i use import os os.system(cmd) but dont work!
21st Dec 2016, 4:09 AM
sam-dev
sam-dev - avatar