【问题标题】:AttributeError: 'str' object has no attribute 'fileno'AttributeError:“str”对象没有属性“fileno”
【发布时间】:2015-07-18 06:58:22
【问题描述】:

代码:

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

可能是什么问题?我正在使用q

【问题讨论】:

标签: python


【解决方案1】:

stdout 参数需要一个文件对象,而不是文件名的字符串。

尝试使用 -

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()

【讨论】:

    猜你喜欢
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 2021-10-04
    • 2019-12-02
    • 2021-09-25
    • 2014-03-04
    相关资源
    最近更新 更多