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.
Related
Does anyone have experience using the subprocess.call command in Python?
I keep getting errors whenever a line like this is in my code:
INFILE1 = open(script_dir+"/Scripts/plot_TSS_profile.R","r")
subprocess.call("Rscript","--slave","--args",filenames["housekeeping_profile"]+" "+filenames["unexpressed_profile"]+" "+filenames["profile_plot"],stdin=INFILE1, stderr=ERR_LOG,stdout=OUT_LOG,shell=True)
INFILE1.close().
If I leave the code as is, I get an error that the program finds multiple values for each th stdin, stderr, and stdout for some reason even though those are the only ones in the code. If I take out these parameters and just put the infile early in the brackets after “--args”, it doesn’t seem to read the file as it says the ‘buffer should be an integer.’
For example, this way gives the buffer error:
INFILE1 = script_dir+"/Scripts/plot_TSS_profile.R"
subprocess.call("Rscript",INFILE1,"--slave","--args",filenames["housekeeping_profile"]+" "+filenames["unexpressed_profile"]+" "+filenames["profile_plot"],stderr=ERR_LOG,shell=True)
INFILE1.close()
Here are my error outputs for more specific information:
The one on buffsize:
Traceback (most recent call last):
File "/mnt/work1/users/pughlab/projects/IEG_MiSEQ/Inferring_DNA_Expression/ExpressionPrediction-master/expression_prediction.py", line 277, in
step5(ERR_LOG,OUT_LOG,args,proj_dir,script_dir,filenames)
File "/mnt/work1/users/pughlab/projects/IEG_MiSEQ/Inferring_DNA_Expression/ExpressionPrediction-master/expression_prediction.py", line 165, in step5
subprocess.call("Rscript",INFILE1,"--slave","--args",filenames["housekeeping_profile"]+" "+filenames["unexpressed_profile"]+" "+filenames["profile_plot"],stderr=ERR_LOG,shell=True)
File "/mnt/work1/software/centos7/python/2.7.15/lib/python2.7/subprocess.py", line 172, in call
return Popen(*popenargs, **kwargs).wait()
File "/mnt/work1/software/centos7/python/2.7.15/lib/python2.7/subprocess.py", line 343, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
And the other error on there being multiple values:
Traceback (most recent call last):
File "/mnt/work1/users/pughlab/projects/IEG_MiSEQ/Inferring_DNA_Expression/ExpressionPrediction-master/expression_prediction.py", line 272, in <module>
step5(ERR_LOG,OUT_LOG,args,proj_dir,script_dir,filenames)
File "/mnt/work1/users/pughlab/projects/IEG_MiSEQ/Inferring_DNA_Expression/ExpressionPrediction-master/expression_prediction.py", line 165, in step5
subprocess.call("Rscript","--slave","--args",filenames["housekeeping_profile"]+" "+filenames["unexpressed_profile"]+" "+filenames["profile_plot"],stdin=INFILE1,stderr=ERR_LOG,stdout=OUT_LOG,shell=True)
File "/mnt/work1/software/centos7/python/2.7.15/lib/python2.7/subprocess.py", line 172, in call
return Popen(*popenargs, **kwargs).wait()
TypeError: __init__() got multiple values for keyword argument 'stdin'
Thank you
The main problem is that call takes a list of arguments or a single string to be parsed by the shell. You don't appear to need shell=True here; just create a single list of all the positional arguments you attempting to pass.
with open(script_dir+"/Scripts/plot_TSS_profile.R","r") as INFILE1:
cmd = [
"Rscript",
"--slave",
"--args",
filenames["housekeeping_profile"],
filenames["unexpressed_profile"],
filenames["profile_plot"]
]
subprocess.call(cmd, stdin=INFILE1, stderr=ERR_LOG, stdout=OUT_LOG)
Passing multiple positional arguments means that parameters that were meant to be set via keyword arguments (like stdin, etc) are being assigned values that were meant as part of the command to execute.
I am trying to invoke a C-program, named “drule.c”, from within my Python-program “drulewrapper.py”. I am trying to use "subprocess" but cannot get it to work.
1) I compile “drule.c” on the Mac’s terminal and all works okay:
$ gcc -o drule drule c
$ ./drule D11
>P>Q>RQ
Fyi, the input -- “D11” -- are axioms in predicate logic; the output -- “>P>Q>RQ” -- is the theorem that is proven and which I then want to process further in my Python program.
2) I write a short Python program (drulewrapper.py) and compile it:
From subprocess import call
def CheckString():
call(“./drule”, “D11”)
3) But when I run CheckString() I get errors:
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
CheckString()
File "/Users/georgeszpiro/Dropbox/metamath/GApl/drulewrapper.py", line 3, in CheckString
call("./drule","D11")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 609, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integerT
Can anybody help?
I am trying to use python's pwntools. I want to start a process using
from pwn import *
s = process('./step1')
When I do this I receive the following error message:
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/pwnlib/tubes/process.py", line 267, in init
stdin, stdout, stderr, master, slave = self._handles(*handles)
File "/usr/local/lib/python2.7/dist-packages/pwnlib/tubes/process.py", line 603, in _handles
tty.setraw(master)
File "/usr/lib/python2.7/tty.py", line 28, in setraw
tcsetattr(fd, when, mode)
termios.error: (22, 'Invalid argument')
I am already in the directory that contains the file step1 and step1 is executable. Does anyone have an idea why I get this error. If it helps, I am using the Linux subsystem on Windows 10.
Check out this link. process() needs its first argument as a list of program arguments. So
$ ./step1 arg1 arg2
is equivalent to
p = process(['step1', 'arg1', 'arg2'])
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
I am trying to replicate C# code in python which executes a thread, waits for it to finish and returns a value. Essentially the method RunAndWait is in a helper class because a call to that method is being made multiple times.
C# code is as follows:
public static bool RunAndWait(Action _action, long _timeout)
{
Task t = Task.Run(() =>
{
Log.Message(Severity.MESSAGE, "Executing " + _action.Method.Name);
_action();
});
if (!t.Wait(Convert.ToInt32(_timeout)))
{
Log.Message(Severity.ERROR, "Executing " + _action.Method.Name + " timedout. Could not execute MCS command.");
throw new AssertFailedException();
}
t.Dispose();
t = null;
return true;
}
In python I have been struggling with a few things. Firstly, there seem to be different types of Queue's where I simply picked the import that seemed to be working import Queue. Secondly, I receive a TypeError as below.
Traceback (most recent call last):
File "C:/Users/JSC/Documents/Git/EnterprisePlatform/Enterprise/AI.App.Tool.AutomatedMachineTest/Scripts/monkey.py",
line 9, in
File "C:\Users\JSC\Documents\Git\EnterprisePlatform\Enterprise\AI.App.Tool.AutomatedMachineTest\Scripts\Libs\MonkeyHelper.py",
line 4, in RunCmdAndWait
TypeError: module is not callable
Here is the python code for monkey:
from Libs.CreateConnection import CreateMcsConnection
import Libs.MonkeyHelper as mh
import Queue
q = Queue.Queue()
to = 5000 #timeout
mh.RunCmdAndWait(CreateMcsConnection, to, q)
serv, con = q.get()
and MonkeyHelper.py:
import threading
def RunCmdAndWait(CmdToRun, timeout, q):
t = threading(group=None, target=CmdToRun, arg=q)
t.start()
t.join(timeout=timeout)
I am not sure what I am doing wrong. I am fairly new to python. Could someone please help me out?
Edit
t = threading.Thread(group=None, target=CmdToRun, args=q)
correcting the line above brought up another error:
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Program Files (x86)\IronPython 2.7\Lib\threading.py", line 552, in _Thread__bootstrap_inner
self.run()
File "C:\Program Files (x86)\IronPython 2.7\Lib\threading.py", line 505, in run
self.target(*self.__args, **self.__kwargs)
AttributeError: Queue instance has no attribute '__len'
Is that because Thread expects multiple args or because the queue is still empty at this point? From what I've seen is that the queue is just being passed as an argument to receive the return value. Is that the right way to go?
Edit2
Changed t = threading.Thread(group=None, target=CmdToRun, args=q) to t = threading.Thread(group=None, target=CmdToRun, args=(q,))
The change yields in a TypeError below, seems weird to me since Thread is expecting a tuple.
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Program Files (x86)\IronPython 2.7\Lib\threading.py", line 552, in _Thread__bootstrap_inner
self.run()
File "C:\Program Files (x86)\IronPython 2.7\Lib\threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
TypeError: tuple is not callable
threading is a module. You likely mean to replace
t = threading(group=None, target=CmdToRun, arg=q)
with
t = threading.Thread(group=None, target=CmdToRun, args=(q,))
args is an argument tuple.