[Tutor] Re subprocess

Steven D'Aprano steve at pearwood.info
Wed Sep 10 12:39:17 CEST 2014


On Wed, Sep 10, 2014 at 11:20:38AM +0200, jarod_v6 at libero.it wrote:
> If I follow the exmple I have this type of error:
> File "./RNA_prova.py", line 73, in run
>     for line in p1.stdout():
> TypeError: 'NoneType' object is not callable

Somehow you have p1.stdout set to None. You can confirm this by 
inserting this line immediately before line 73 with:

    print p1.stdout

which will almost certainly print "None" just before the exception.

More comments below:


> This is the class I use:
> def run(cmd,pi):
> 		import subprocess
> 		import time
> 		import logging

This is not a class, it is a function ("def", rather than "class"). 
Also, it is normally a good idea to have all your imports done once, at 
the top of the module, rather than inside a function or method. And it 
is very unusual to use two tabs for a single indent level. It is 
recommended to use one tab or 4 spaces.

> 		logging.basicConfig(level=logging.DEBUG,format="%(asctime)s - %(name)s - %
> (levelname)s - %(message)s")
> 		#logging.debug(" Running pipelines: %s" % (cmd))
> 		# setup logging
> 		log_file = "None"

Here you set the log_file to the string "None". What is the purpose of 
this? It seems like a strange thing to do.


> 		tou = "file"+"_.log.txt"
> 		if log_file is not None:

Since you have just set the variable log_file to a string, it CANNOT 
possibly be None. So this clause will ALWAYS run, and the "else" clause 
is dead code that cannot possibly occur.

*Provided* that this is the code you are actually running. I doubt this 
is actually the code you are running, since the error shown tells you 
that p1.stdout is set to None, which requires the else clause to run. So 
I think that this code you show us and the code you are actually running 
are not the same.

Am I right?


> 			logfh = open(tou, "w")
> 		else:
> 			logfh = None
> 			print "####################################################"
> 		p1 = subprocess.Popen(cmd,shell=True,stdout=logfh,stderr=logfh,cwd=pi)

For p1.stdout to be set to None, logfh must be set to None. You can 
check that by putting:

		print logfh, p1.stdout

here.


Please stop replying to Digests without trimming the hundreds and 
hundreds of lines of irrelevent quoted messages.



-- 
Steven


More information about the Tutor mailing list