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
Related
This question already has answers here:
What does "sys.argv[1]" mean? (What is sys.argv, and where does it come from?)
(9 answers)
Closed 6 months ago.
I'm having trouble figuring out how the program is supposed to read in the cvs file.
Here is the error:
Traceback (most recent call last):
File "/Users/myname/Desktop/sealsort/sealsort.py", line 9, in <module>
newm_path = sys.argv[1] #'./s18_new.csv'
IndexError: list index out of range
As noted in the link posted by mkrieger1, the sys.argv[1] is referencing the first command line argument.
So instead of running a command like this:
python program.py
You need to run it with three arguments like this:
python program.py /path/To/first.csv /path/To/second.csv /path/To/third.csv
Note: Line 5 gives a use example
python sealsort.py /path/to/newm/survey /path/to/retm/survey /path/to/school/doc
It seems you are trying to run this script on a macOS machine. And I'm supposing you have the three CSV files in the same folder as the Python script.
You need to navigate, via terminal, to the folder the files are stored. So, first open the Terminal application, then navigate to the folder with this command: cd /Users/myname/Desktop/sealsort (here I'm using the same path that is in your question), then you will need to execute the script as described in the first comments:
# USE LIKE:
# python sealsort.py /path/to/newm/survey /path/to/retm/survey /path/to/school/doc
Supposing the files are: s18_new.csv, s18_return.csv, s18_schools.csv, execute the script specifying the name of these files as arguments to the program. If you do not specify any of the required arguments, one of the elements at the indexes 1, 2, and 3 will not be found and you will get that error (IndexError: list index out of range).
So, the correct command would be: python sealsort.py ./s18_new.csv ./s18_return.csv ./s18_schools.csv
This way, the element at index 0 (of argv) will be sealsort.py, the element 1 will be s18_new.csv, 2 will be s18_return.csv, and 3 will be s18_schools.csv.
I hope this helps.
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.
This question already has answers here:
how to direct output into a txt file in python in windows
(6 answers)
Closed 6 years ago.
I am running a python script which checks for the modifications of files in a folder. I want that output to be printed in a file. The problem is that the output is DYNAMIC , the cmd is always open and when a file is modified, I will have an information right-ahead about that in the cmd window. All the solutions which I found were matching the situations were I just run a command and I finish with that.
I tryed with:
python script.py > d:\output.txt but the output.txt file is empty
An example of the command prompt windows, after I run the command python script.py and I touch the 2 files, the command prompt will look like this. I want to capture that output.
Solution: In the python script which I use, add to the logging.basicConfig function, one more argument : filename='d:\test.log'
The issue is output buffering. If you wait long enough, you'll eventually see data show up in the file in "blocks". There are a few ways around it, for example:
Run python with the -u (unbuffered) flag
Add a sys.stdout.flush() after all print statements (which can be simplified by replacing stdout with a custom class to do it for you; see the linked question for more)
Add flush=True option to print statements if your version of Python supports it
If appropriate, use the logging module instead of print statements.
python test.py>test.txt
It's working for me in windows cmd prompt
As I see it the simplest would be to add the file handling (the writing to output.txt ) inside your script. Thus, when it is time to print the information you need to have (as your example shows when you touch two files you print two lines), you can open the file, write the specific line and close it after it is done (then you can see the updated output.txt).
Get the file path for the output.txt as a command line argument like
python script.py --o 'd:\output.txt'
for example.
This question already has answers here:
Python command line 'file input stream'
(3 answers)
Closed 8 years ago.
Is it possible to run a python script and feed in a file as an argument using <? For example, my script works as intended using the following command python scriptname.py input.txt and the following code stuffFile = open(sys.argv[1], 'r').
However, what I'm looking to do, if possible, is use this command line syntax: python scriptname.py < input.txt. Right now, running that command gives me only one argument, so I likely have to adjust my code in my script, but am not sure exactly how.
I have an automated system processing this command, so it needs to be exact. If that's possible with a Python script, I'd greatly appreciate some help!
< file is handled by the shell: the file doesn't get passed as an argument. Instead it becomes the standard input of your program, i.e., sys.stdin.
When you use the < operator in a shell you are actually opening the file and adding its contents to your scripts stdin
However there is is a python module that can do both. It's called fileinput.
https://docs.python.org/2/library/fileinput.html
It was shown in this post
How do you read from stdin in Python?
You can use the sys module's stdin attribute as a file like object.
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