by
0
5
3,499
1
Top 1% !
Popular
Famous
Tagged
Specified
Popularity: 3573rd place
Created
Modified Jul 25, 2013
This snippet is Public
Architecturex86_64
LanguagePython
LicenseMIT_X11
OSLinux

Python execute shell cmd

This python script runs a shell in linux and executes the cmd or script in a new thread. Python polls the thread till it has some output and save it to file, as well as standard error redirection to file. Then close both file descriptors.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/297/Python-execute-shell-cmd?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
logFile = open(outputDir + '/build_log.txt',"w") errFile = open(outputDir + '/build_err.txt',"w") compileProcess = Popen(<CMD_or_SCRIPT>, shell=True, executable='/bin/tcsh', stdout=PIPE, stderr=errFile) while (True): output = compileProcess.stdout.readline() if output == "": break logFile.write(output) logFile.close() errFile.close()
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus