How to change audio with subprocess? - python

I want to make a music player with tkkinter that plays .wav files. I created everything, but a volume slider. I created the volume slider but I wanted to know how to change the volume in sub process. I tried this code :
from subprocess import call
call(["amixer", "-D", "pulse", "sset", "Master", "0%"])
When I ran it I got the following error : "Traceback (most recent call last):
File "C:/Users/Parent/Desktop/Python/volume chang.py", line 2, in <module>
call(["harvard.wav", "-D", "pulse", "sset", "Master", "0%"])
File "C:\Users\Parent\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 323, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\Parent\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Users\Parent\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application
"
I wanted to know if I could fix this and be able to change volume.

Related

OSError: [Errno 8] Exec format error with osm-bundler

I'm trying to get osm-bundler to work, but I run into this problem:
Traceback (most recent call last):
File "RunBundler.py", line 9, in <module>
manager.preparePhotos()
File "/Users/PeterE/Downloads/osm-bundler/osmbundler/__init__.py", line 121, in preparePhotos
self._preparePhoto(photoInfo)
File "/Users/PeterE/Downloads/osm-bundler/osmbundler/__init__.py", line 176, in _preparePhoto
self.extractFeatures(photo)
File "/Users/PeterE/Downloads/osm-bundler/osmbundler/__init__.py", line 244, in extractFeatures
self.featureExtractor.extract(photo, self.photoDict[photo])
File "/Users/PeterE/Downloads/osm-bundler/osmbundler/features/siftvlfeat.py", line 16, in extract
subprocess.call([self.executable, "%s.jpg.pgm" % photo, "-o", "%s.key" % photo])
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 523, in call
return Popen(*popenargs, **kwargs).wait()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
This is caused when I run this command:
python RunBundler.py --photos="/Users/PeterE/Desktop/osmbundlertest"
I can put in any relevant lines from the Python scripts, but I have no idea which one it is now. The output before the error is:
Working directory created: /var/folders/rq/5mypbx554jgflr6c8097zltc0000gn/T/osm-bundler-CXSOEi
BundlerMatching executable path: /Users/PeterE/Downloads/osm-bundler/software/bundler/bin/KeyMatchFull
Sift executable path: /Users/PeterE/Downloads/osm-bundler/software/vlfeat/bin/glx/sift
Processing photo 'IMG_0055.JPG':
Entry for the camera 'Apple', 'iPod touch' does not exist in the camera database
Can't estimate focal length in pixels for the photo '/Users/PeterE/Desktop/osmbundlertest/IMG_0055.JPG'
Copy of the photo has been scaled down to 1200x896
Extracting features with the SIFT method from VLFeat library...

subprocess error message:[Errno 2] in _execute_child raise child_exception

In my program I call the command:
command_two = 'sfit4Layer0.py -bv5 -fs'
subprocess.call(command_two.split(), shell=False)
I am using PyCharm and I get the error message:
Traceback (most recent call last):
File "part2test.py", line 5, in <module>
subprocess.call(command_two.split(), shell=False) #writes the summary file
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
When walking through my program, it never gets to the program I want it to sfit4Layer0.py, it is getting stuck in subprocess but I am not sure why. Changing the shell=True doesn't do anything helpful either - I don't get these error messages but it does not execute my code properly. Any suggestions would be helpful.
My bash profile:
PATH="~/bin:/usr/bin:${PATH}"
export PATH PYTHONPATH="/Users/nataliekille/Documents/sfit4/pbin/Layer0:/Users/nataliekille/Documents/sfit4/pbin/Layer1:/Users/nataliekille/Documents/sfit4/pbin/ModLib:/Users/nataliekille/Documents/sfit4/SpectralDB"
export PYTHONPATH
PATH=${PATH}:${PYTHONPATH}
export PATH
You've missed an important part of the subprocess documentation. "If passing a single string [at the command, rather than a list of strings], either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments."
So the kernel is compaining because there is not executable with the name 'sfit4Layer0.py -bv5 -fs'. Should work if you replace the string with (for example) 'sfit4Layer0.py -bv5 -fs'.split(), or ['sfit4Layer0.py', '-bv5', '-fs'].

Calling Popen in Python on a MAC - Error can not find file

When I try to shell out of my Python 3.51 program to run the Popen command I get the following errors. Yet when I copy the exact string I'm passing to Popen to the Terminal command line it works fine and opens the file in Adobe Reader which is my default app for the .pdf files.
Here is the Code:
finalCall = r'open /Users/gbarnabic/Documents/1111/combined.pdf'
print(finalCall)
pid_id = subprocess.Popen(finalCall).pid
Here is the error:
open /Users/gbarnabic/Documents/1111/combined.pdf
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/init.py", line 1549, in call
return self.func(*args)
File "pdfcomb2.py", line 212, in change_dir
self.openPDF(outFileName, pageNum)
File "pdfcomb2.py", line 426, in openPDF
subprocess.run(finalCall)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 696, in run
with Popen(*popenargs, **kwargs) as process:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 950, in init
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1544, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'open /Users/gb/Documents/1111/combined.pdf'
Georges-MBP:filepicktest gb$ open /Users/gb/Documents/1111/combined.pdf
Georges-MBP:filepicktest gb$
With Popen you need to set shell=True to pass command as a string or split command in a list of arguments. Could be done with shlex
import shlex
import subprocess
subprocess.Popen(shlex.split('open ....'))
You could check example in documentation:
https://docs.python.org/2/library/subprocess.html#subprocess.Popen
So the error here means that Python try to run file with name open /Users/gb/Documents/1111/combined.pdf. Obviously it doesn't exist

Python interface wifi.Cell.all(Interface) giving system error

I am trying to connect to the wireless network using wifi. As per the documents I have written the code, but it gives system error that file not found. As I can see the argument description of the Cell.all() is interface but we are providing literal as argument. What might be the problem?
import time
import datetime
from wifi import Cell, Scheme
while True :
print datetime.datetime.now()
print Cell.all('wlan0')
time.sleep(5)
>>>
2015-01-25 12:38:43.784000
Traceback (most recent call last):
File "D:\Documents\Development\Script\AutoLogon.py", line 9, in <module>
print Cell.all('wlan0')
File "C:\Python27\lib\site-packages\wifi\scan.py", line 29, in all
stderr=subprocess.STDOUT)
File "C:\Python27\lib\subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>>
The python interface wifi was developed for Linux. I may be able to use the library using cygwin.

Compiling and Executing Java file in python

how can I open an java file in python?, i've search over the net and found this:
import os.path, subprocess
from subprocess import STDOUT, PIPE
def compile_java (java_file):
subprocess.check_call(['javac', java_file])
def execute_java (java_file):
cmd=['java', java_file]
proc=subprocess.Popen(cmd, stdout = PIPE, stderr = STDOUT)
input = subprocess.Popen(cmd, stdin = PIPE)
print(proc.stdout.read())
compile_java("CsMain.java")
execute_java("CsMain")
but then I got this error:
Traceback (most recent call last):
File "C:\Python33\lib\subprocess.py", line 1106, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\casestudy\opener.py", line 13, in <module>
compile_java("CsMain.java")
File "C:\casestudy\opener.py", line 5, in compile_java
subprocess.check_call(['javac', java_file])
File "C:\Python33\lib\subprocess.py", line 539, in check_call
retcode = call(*popenargs, **kwargs)
File "C:\Python33\lib\subprocess.py", line 520, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Python33\lib\subprocess.py", line 820, in __init__
restore_signals, start_new_session)
File "C:\Python33\lib\subprocess.py", line 1112, in _execute_child
raise WindowsError(*e.args)
FileNotFoundError: [WinError 2] The system cannot find the file specified
>>>
the python file and java file is in the same folder, and I am using Python 3.3.2, how can I resolve this? or do you guys have another way on doing this?, any answer is appreciated thanks!
I think it isn't recognizing the javac command. Try manually running the command and if javac isn't a recognized command, register it in your PATH variable and try again.
Or you could just try typing the full pathname to the Java directory for javac and java.
you need to add path to your java file name. like this:
compile_java("C:\\path\to\this\CsMain.java")

Categories