How to get output of command line to python [duplicate] - python

This question already has answers here:
Retrieving the output of subprocess.call() [duplicate]
(7 answers)
Closed 8 years ago.
I run windows command line programm from python, the command line programm return strings, for example: I run that line
subprocess.call("RPiHubCMDTool.exe dev", shell=True)
and I see in cmd window the output dev0 FT2232H RPi HUB Module A 136241 A ,
dev1 FT2232H RPi HUB Module B 136242 B. I whant to work in python with that output. How to bring it from cmd window to python? Could you provide an example?

to get the output you can use
output=subprocess.check_output(["echo", "Hello World!"])
print output
# Hello World!

How about write the result to a file and read this file in python?
subprocess.call("RPiHubCMDTool.exe dev > result.txt", shell=True)
f = open('result.txt', 'r')
# do something with f

Related

How to execute a python script and write output to a file? [duplicate]

This question already has answers here:
Output a python script to text file
(4 answers)
Closed 5 years ago.
i have the following script in python with a while loop
from time import sleep
while True:
print "hola"
print "mundo"
sleep(2)
and i want to write the output to a file with the following code:
import subprocess
with open("output.log", "w") as output:
subprocess.call(["python", "./main.py"], stdout=output);
the thing is that the while never ends, the file output.log never gets the output from the script, i wonder if there is a way to do it.
You can simply do it by the following command.
python filename.py > output.log
The above command works for both linux and windows.

Python subprocess.call not using all arguments [duplicate]

This question already has answers here:
Understanding python subprocess.check_output's first argument and shell=True [duplicate]
(2 answers)
Closed 6 years ago.
I'm trying to run this code that should open the selected mp3 with VLC, use Line 1 as the audio output, and close when it's done. But the arguments don't all seem to be getting through.
Python code
import subprocess
vlcpath = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe"
audiopath = "C:\\Users\\Aidan\\Desktop\\test.mp3"
args = [vlcpath, audiopath, "--aout=waveout", '--waveout-audio-device="Line 1 (Virtual Audio Cable) ($1,$64)"', "--play-and-exit"]
subprocess.call(args, shell=True)
for i in args: #for diagnostic purposes
print(i)
Which should run similarly to this command line input
"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" C:\Users\Aidan\Desktop\test.mp3 --aout=waveout --waveout-audio-device="Line 1 (Virtual Audio Cable) ($1,$64)" --play-and-exit
The command line input plays and exits properly, and plays to Line 1. The python code plays and exits, but not to Line 1.
Edit: I should mention I'm using python 3.4.4
Since shell is true the string representation of the list is passed to the command line which is not what you want. Look closely at the examples on this page https://docs.python.org/2/library/subprocess.html

How to run script over windows without any install [duplicate]

This question already has answers here:
How to compile python script to binary executable
(4 answers)
Closed 6 years ago.
if i create python code with .py and i want my friends get the code and it will work on theme computer (open files and print them).
how can i do that without install.
i mean i dont wont them yto install python.
i can do it as .exe or something?
thanks!
my code is stupid but required here so -
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# Close opend file
fo.close()
I would use Py2exe
but there are more solutions here are two links
link 1
link 2

Sending print output to a file in Python script [duplicate]

This question already has answers here:
Correct way to write line to file?
(17 answers)
Closed 7 years ago.
How would I go about sending the output of a print command to a new file? I have a python script where I need to redirect the output at the end of the print statement to a file but I can't seem to find a way to accomplish the redirect. Why doesn't "print (stuff to be redirected) > newfile.txt" work?
Any help is appreciated!
As mentioned in this post, you could set the standard output to a file object.
import sys
sys.stdout = open('file', 'w')
Then, all your print statements should go directly to that file.

Getting the output of os.system in python and processing it after [duplicate]

This question already has answers here:
Python: How to get stdout after running os.system? [duplicate]
(6 answers)
Closed 8 years ago.
I am trying to do something like:
f = subprocess.check_output("./script.sh ls -l test1/test2/test.log", shell=True)
when I print f, I get value 0. I tried using subprocess and then read() and even then i dont get the details of the file. I need to verify the size of the file..
Not sure how it can be done.
Any help?
When I used
f = os.system("./script.sh ls -l test1/test2/test.log"), I get the output but does not get saved in f. Something like stdoutput or something..
UPDATED:
I used
f = os.popen("./script.sh ls -l test1/test2/test.log 2>&1")
if I ran the same command in quotes above, directly on CLI myself, it works fine but if I used the above in a script OR used s = f.readline(), the script stops, I need to hit "return" before the script can proceed..
Why is that? I need 's' because I need to process it.
You can use subprocess.check_output:
f = subprocess.check_output("./script.sh ls -l test1/test2/test.log",shell=True)
print(f)
You can split into a list of individual args without using shell=True:
f = subprocess.check_output(['./script.sh', 'ls', '-l', 'test1/test2/test.log']))

Categories

Resources