subprocess.Popen shell=True to shell=False
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping
--
Chapters
00:00 Subprocess.Popen Shell=True To Shell=False
00:28 Accepted Answer Score 8
00:48 Answer 2 Score 0
01:01 Thank you
--
Full question
https://stackoverflow.com/questions/4147...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #shell #subprocess
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping
--
Chapters
00:00 Subprocess.Popen Shell=True To Shell=False
00:28 Accepted Answer Score 8
00:48 Answer 2 Score 0
01:01 Thank you
--
Full question
https://stackoverflow.com/questions/4147...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #shell #subprocess
#avk47
ACCEPTED ANSWER
Score 8
You can't directly use piping in the command the way you do with shell=True, but it's easy to adapt:
with open(file_name, 'ab') as outf:
proc = subprocess.Popen(['candump', '-tA', 'can0', 'can1'], stdout=outf)
That opens the file at the Python level for binary append, and passes it as the stdout for the subprocess.
ANSWER 2
Score 0
Only shell mode supports inline piping operators so you'll need to do the redirection manually. Also, you'll need to split your command line into individual arguments which you can either do manually or have the shlex module do for you:
subprocess.Popen(shlex.split('candump -tA can0 can1'), stdout=open(file_name, 'ab'))