Python 属性错误:';str';对象没有属性';文件编号';

Python 属性错误:';str';对象没有属性';文件编号';,python,Python,代码: 错误: 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"

代码:

错误:

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()
$python processLog.py
回溯(最近一次呼叫最后一次):
文件“processLog.py”,第11行,在
printit()
文件“processLog.py”,第9行,在printit中
p=subprocess.Popen([“python”、“q”、“-H”、“-d”、“,”,query],stdout=fileName)
文件“/usr/lib/python2.7/subprocess.py”,第702行,在__
errread,errwrite),to_close=self._get_句柄(stdin,stdout,stderr)
文件“/usr/lib/python2.7/subprocess.py”,第1128行,在获取句柄中
c2pwrite=stdout.fileno()
AttributeError:“str”对象没有属性“fileno”

可能是什么问题?我正在使用参数
stdout
需要一个file对象,而不是文件名的字符串

试用-

$ 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'

请注意,StringIO虽然属于适当的子类,但不会工作。有关更多信息,请参阅。
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()