Python 如何让Popen写入文件

Python 如何让Popen写入文件,python,popen,fwrite,python-2.6,Python,Popen,Fwrite,Python 2.6,因此,我制作了和IP/主机名扫描程序,并尝试在运行ping或未能运行ping后将输出打印到文件中。我的问题是我遇到了以下错误: Traceback (most recent call last): File ".\IPlookup.py", line 59, in <module> print >> IPtext," is down!", 'Filename:', filename AttributeError: 'str' object has no att

因此,我制作了和IP/主机名扫描程序,并尝试在运行ping或未能运行ping后将输出打印到文件中。我的问题是我遇到了以下错误:

Traceback (most recent call last):
  File ".\IPlookup.py", line 59, in <module>
    print >> IPtext," is down!", 'Filename:', filename
AttributeError: 'str' object has no attribute 'write'

Traceback (most recent call last):
 File ".\IPlookup.py", line 55, in <module>
   print >> output, 'Filename:', filename
AttributeError: 'Popen' object has no attribute 'write'
有没有办法让str输出和popen都写入文件,或者我需要完全更改代码

通过这样做解决了我的一部分问题

elif userType == '-l':
    with open('out.txt','a') as f:
        IPtext = raw_input("Please enter IP or URL: ")
        response = os.system("ping -c 1 " + IPtext)
        output = subprocess.Popen(['nslookup',IPtext])
        if response == 0:
            f.write(output)
            f.close()
        else:
            f.write(IPtext)
            f.close()
现在唯一不起作用的是打印出弹出错误的popen

TypeError:参数1必须是字符串或只读字符缓冲区,而不是Popen

    elif userType == 't -l' or userType == 't --logfile':
       with open('Pass.txt','a') as f:
       IPtext = raw_input("Please enter IP or URL: ")
       response = os.system("ping -c 1 " + IPtext)
       merp = subprocess.Popen(['nslookup',IPtext], stdout=subprocess.PIPE)
       out, err = merp.communicate()
       if response == 0:
           f.write('\n')
           f.write(out)
           f.close()
       else:
           with open('Fail.txt','a') as f:
               f.write('\n')
               f.write(IPtext)
               f.close()

如果我正确理解了您的问题,那么
f.write(output)
的行将抛出TypeError

这是因为在您的案例中,输出是一个Popen对象。 更换这些线路:

output = subprocess.Popen(['nslookup',IPtext])
    if response == 0:
        f.write(output)
有了这些:

# ... everything before your .subprocess.Popen(...) line
popen = subprocess.Popen(['nslookup',IPtext], stdout=subprocess.PIPE)# this is executed asynchronus
popen.wait() # this is to wait for the asynchron execution
resultOfSubProcess, errorsOfSubProcess = popen.communicate()
# resultOfSubProcess should contain the results of Popen if no errors occured
# errorsOfSubProcess should contain errors, if some occured
    if response == 0:
        f.write(resultOfSubProcess)
        # ... the rest of your code
编辑:在继续之前,您可能还希望检查resultOfSubProcess变量是否为空或errorsOfSubProcess中是否存在错误

# ... everything before your .subprocess.Popen(...) line
popen = subprocess.Popen(['nslookup',IPtext], stdout=subprocess.PIPE)# this is executed asynchronus
popen.wait() # this is to wait for the asynchron execution
resultOfSubProcess, errorsOfSubProcess = popen.communicate()
# resultOfSubProcess should contain the results of Popen if no errors occured
# errorsOfSubProcess should contain errors, if some occured
    if response == 0:
        f.write(resultOfSubProcess)
        # ... the rest of your code