traceback while using subprocess.call - python

import sys
import subprocess
arg1= sys.argv[1]
subprocess.call("inversion_remover.py",arg1)
subprocess.call("test3.py")
subprocess.call("test4.py")
I am getting the following traceback
Traceback (most recent call last):
File "parent.py", line 4, in <module>
subprocess.call("inversion_remover.py",arg1)
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 659, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
How do I solve the above traceback?

You need to pass in the command as a list:
subprocess.call(["inversion_remover.py", arg1])
subprocess.call(["test3.py"])
subprocess.call(["test4.py"])
otherwise your arg1 value is passed on to the underlying Popen() object as the bufsize argument.
Note that the scripts must be found on the path. If you want to execute these files from the local directory either prefix the path with ./, or extend the PATH environment variable to include the current working directory:
subprocess.call(["./inversion_remover.py", arg1])
subprocess.call(["./test3.py"])
subprocess.call(["./test4.py"])
or
import os
env = os.environ.copy()
env['PATH'] = os.pathsep.join(['.', env['PATH']])
subprocess.call(["inversion_remover.py", arg1], env=env)
subprocess.call(["test3.py"], env=env)
subprocess.call(["test4.py"], env=env)

Related

TypeError: bufsize must be an integer?

I am making a small program where I can open a file from any part of the computer with it's default editor. This is my code:
from os import *
import subprocess
print("Welcome to my File Finder. Here you can search for a file and open it.")
file_name = str(input("Your file's name:"))
print(subprocess.call(["xdg-open"], file_name))]
But instead of opening, it return this error:
Traceback (most recent call last):
File "Important_tester_projects.py", line 6, in <module>
print(subprocess.call(["xdg-open"], file_name))
File "/usr/lib/python3.6/subprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.6/subprocess.py", line 609, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
I have googled to find a solution for this error, but I can't find any that seems to solve my Problem. How can fix my error?
NOTE: My Linux OS uses XFCE, not Gnome.
Instead, use subprocess.check_output(). Since your command has multiple words, parse your command with split() method from shlex lib.
Something like this:
import subprocess
import shlex
cmd=shlex.split('[find][2] root_dir -name file_name')
print subprocess.check_output(cmd)

Python subprocess call throws error when writing file

I'd like to use SVOX/pico2wave to write a wav-file from Python code. When I execute this line from a terminal the file is written just fine:
/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."
I've verified that pico2wave is located in /usr/bin.
This is my Python code:
from subprocess import call
call('/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."')
... which throws this error:
Traceback (most recent call last):
File "app/app.py", line 63, in <module>
call('/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."')
File "/usr/lib/python2.7/subprocess.py", line 168, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1024, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
From the documentation
Providing a sequence of arguments is generally preferred, as it allows
the module to take care of any required escaping and quoting of
arguments (e.g. to permit spaces in file names). If passing a single
string, either shell must be True (see below) or else the string must
simply name the program to be executed without specifying any
arguments.
So you might try with
call(['/usr/bin/pico2wave', '-w=/tmp/tmp_say.wav', '"Hello world."'])

Python Subprocess clarification

I am trying to execute a program with python using subprocess
The format our professor gave us was subprocess(path/executableProgram)
File: OS377.py
I am doing it as subprocess(['/home/Joseph/OS377.py']) but I am getting errors
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.2/subprocess.py", line 471, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python3.2/subprocess.py", line 745, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.2/subprocess.py", line 1361, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error
I need to execute a file using this format but am unsure how to go about it
Code:
def RUN(file):
pid = os.fork() #pid is non-zero in the parent process and 0 in the child
if pid: #parent
os.waitpid(pid,0)
elif pid == 0: #child
print("path is : ")
child(file)
def child(file):
#path = os.path.abspath(file)
#print(path)
subprocess.call(os.path.abspath('OS377.py'))
subprocess.call(['python', os.path.abspath('OS377.py')].
The file you're calling, a python script, may not be marked as executable (chmod u+x file.py). Alternatively, you should probably be executing it with $ python file.py - which is calling the python interpreter and passing it the name of your script as the first argument. So you should put it as subprocess.call(['python', '/home/Joe/file.py'].
Btw, did you mean subprocess.call() instead of just subprocess()?

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")

python subprocess.call arguments

on Linux I have the following python source file called visca.py:
from subprocess import call
def recall(preset):
call(["visca-cli", "memory_recall", str(preset)])
I open python interpreter in shell and import visca, then i type visca.recall(0) and get
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "visca.py", line 13, in recall
subprocess.call(["visca-cli", "memory_recall", str(preset)]) File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait() File "/usr/lib/python2.7/subprocess.py", line 629, in __init__
raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer
However, if I type directly in python shell
>>> from subprocess import call
>>> call(["visca-cli", "memory_recall", "0"])
10 OK - no return value
0
it works. What's the problem?
It's telling you that bufsize must be an integer. I am going to guess that whatever value you set for preset in the script is not an integer (keep in mind that 0.0 is a float, not an integer). Do a quick check for what your argument is by printing it out in the function.

Categories