I'm a beginner with python trying to run multiple commands in one subprocess call.
This is my code:
import subprocess, sys, time
print ("Python OS 1.0")
print ("Using Python",sys.version)
livecommand = input(">>")
output = subprocess.call(livecommand,'time.sleep(10000.00)',shell=True)
print (output)
Error:
Traceback (most recent call last): File
"C:\Users\John\Desktop\OS\FILES\console.py", line 8, in
output = subprocess.call(livecommand,'time.sleep(10000.00)',shell=True) File
"C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py",
line 267, in call
with Popen(*popenargs, **kwargs) as p: File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py",
line 607, in init
raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer
Related
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)
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)
import subprocess
import sys
f = open('IPList.txt')
for line in f:
subprocess.call("nslookup", line, shell=True)
#print (line)
f.close()
Above program doesn't work. Getting below error:
======================================================================
PS C:\Python34> .\Python testnslookup.py
Traceback (most recent call last):
File "testnslookup.py", line 7, in <module>
subprocess.call("nslookup", line, shell=True)
File "C:\Python34\lib\subprocess.py", line 537, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Python34\lib\subprocess.py", line 767, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
=======================================================================
Try putting all of the call non-keyword arguments in a list.
subprocess.call(["nslookup", line], shell=True)
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.
Can anyone explain why I get this error if I run the communicate function twice?
For instance
from subprocess import *
SVN=Popen('which svn', shell=True, stdout=PIPE)
print SVN.communicate()[0]
returns
"/usr/bin/svn"
but running communicate again...
print SVN.communicate()[0]
returns...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 746, in communicate
stdout = _eintr_retry_call(self.stdout.read)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 478, in _eintr_retry_call
return func(*args)
ValueError: I/O operation on closed file
Because the "file", which is actually the stdout of the program being invoked, has been closed. This means you have already read all the output in the previous communicate(), so calling it again can never produce anything.