Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'str' object has no attribute 'fileno'

Tags:

python

Code:

import subprocess

def printit():
    for i in range(6):
        for j in range(6):
            query = "select rxpkts, txpkts from ./log.csv where datapath = "+str(i)+" and port = "+str(j)
            fileName = str(i)+"_"+str(j)+".csv"
            with open(fileName, "w+"):
                p = subprocess.Popen(["python", "q", "-H", "-d", ",", query], stdout=fileName)

printit()

Error:

$ python processLog.py 
Traceback (most recent call last):
  File "processLog.py", line 11, in <module>
    printit()
  File "processLog.py", line 9, in printit
    p = subprocess.Popen(["python", "q", "-H", "-d", ",", query], stdout=fileName)
  File "/usr/lib/python2.7/subprocess.py", line 702, in __init__
    errread, errwrite), to_close = self._get_handles(stdin, stdout, stderr)
  File "/usr/lib/python2.7/subprocess.py", line 1128, in _get_handles
    c2pwrite = stdout.fileno()
AttributeError: 'str' object has no attribute 'fileno'

What could be the issue? I am using q

like image 756
sinhayash Avatar asked Jul 18 '15 06:07

sinhayash


People also ask

How do I fix AttributeError str object has no attribute?

The Python "AttributeError: 'str' object has no attribute" occurs when we try to access an attribute that doesn't exist on string objects. To solve the error, make sure the value is of the expected type before accessing the attribute.

What is the attribute error in Python?

AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. For example, if we take a variable x we are assigned a value of 10. In this process suppose we want to append another value to that variable. It's not possible.


1 Answers

The stdout argument needs a file object, not the string for the filename.

Try using -

import subprocess

def printit():
    for i in range(6):
        for j in range(6):
            query = "select rxpkts, txpkts from ./log.csv where datapath = "+str(i)+" and port = "+str(j)
            fileName = str(i)+"_"+str(j)+".csv"
            with open(fileName, "w+") as f:
                p = subprocess.Popen(["python", "q", "-H", "-d", ",", query], stdout=f)

printit()
like image 104
Anand S Kumar Avatar answered Sep 17 '22 01:09

Anand S Kumar