using python subprocess's call function, but not getting output file [duplicate] - python

This question already has answers here:
How to redirect output with subprocess in Python?
(6 answers)
Python: How do I redirect this output?
(2 answers)
Closed 8 years ago.
I'm using python subprocess library to run command line in python file.
After importing the library, I used following code to store the output
call(["python", "make.py", ">", "data"])
But for some reason, I didn't get data file

You have to modify the stdout , check the official document subprocess
import subprocess
my_output_file = open("/home/user/output", "a")
subprocess.call(["python", "hello.py"],stdout=my_output_file)
my_output_file.close()

Related

How to write "subprocess.run" results to a log file without using "shell=True"? [duplicate]

This question already has answers here:
How do I redirect stdout to a file when using subprocess.call in python?
(2 answers)
Closed 2 years ago.
Currently, I am using the following format to write the run results to a log file.
p = subprocess.run(["mpiexec -n 2 ./executor >log"],shell=True)
Could anyone tell me how to avoid using the "shell=True" while I can write a log file?
Thank you.
Just split the arguments yourself, open the file yourself, and pass the open file to run to make it send the output there:
with open('log', 'wb') as outfile:
p = subprocess.run(['mpiexec', '-n', '2', './executor'], stdout=outfile)

How to access both incoming stdout and stderr pipelines from inside python? [duplicate]

This question already has answers here:
Piping both stdout and stderr in bash?
(2 answers)
What does " 2>&1 " mean?
(19 answers)
Closed 3 years ago.
In a bash script, I can access the pipes interchangeably. If I would like to do this from within a python script, how would I go about accessing sys.stderr?
Currently, I am doing the following:
#/usr/bin/env python
import sys
std_input = sys.stdin.readlines()
std_error = sys.stderr.readlines() # <-- fails, complaining that the file is
# not open for reading.
And I call the code from within a bash script:
#/usr/bin/env bash
... | ./python-script.py
Where there exists both stdin and stderr content on the pipeline streaming into the python-script.py command.
There is no need for concurrency or anything fancy.

Convert oga (Telegram audio file fpormat) to ogg/wav file format using Python [duplicate]

This question already has answers here:
How to convert Telegram voice in a wave file in python
(2 answers)
Closed 3 years ago.
Not able to proceed ahead.
We get .oga file from record in Telegram app.
Which needs to be converted to wav/ogg file format for further processing.
Taking in consideration, that this answer is purely academic, I had not try it. You can use ffmpeg, and run it through python with subprocess.
import subprocess
src_filename = 'source.ext1'
dest_filename = 'output.ext2'
process = subprocess.run(['ffmpeg', '-i', src_filename, dest_filename])
NOTE: Looking around, I found this that may be the same that you asked.

os.system not writing in terminal [duplicate]

This question already has answers here:
Python: How to get stdout after running os.system? [duplicate]
(6 answers)
Closed 6 years ago.
i have a simple test file i created in order to use vmd (a program for my job)
This test file is as simple as :
import os
os.system("vmd -eofexit < VMD_script.tcl -args 3spi_cholesterol")
Basically, im using os.system to launch a program name vmd with another script i wrote and im giving it one argument. What i found it is that when i run this test script, i get nothing done but if i just go in terminal and write :
vmd -eofexit < VMD_script.tcl -args 3spi_cholesterol
everything works perfectly. Is there anything im doing wrong with os.system? I have been using this line for a while now but on linux and it was working perfectly, could it be a mac issue?
Thanks allot
import subprocess
ls_output = subprocess.check_output(['vmd', '-eofexit', '<', 'VMD_script.tcl', '-args', '3spi_cholesterol'])

How to run dos commands in python? [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 8 years ago.
can we write a script in python that runs dos commands/dos mode?
Eg: I have a command
megarec.exe -cleanflash .1
which supposed to run only on dos mode.I do it manually.Now i need to write a script which automates the above procedure.can i do it?
import subprocess
ch=subprocess.Popen("megarec.exe -cleanflash .1",shell=True,stdout=subprocess.PIPE,stderr=subrocess.PIPE)
output,err=ch.communicate()
if output:
print "success:",output
else:
print "error:",error

Categories