简体   繁体   English

莫名其妙的ResourceWarning:未关闭的文件<_io.TextIOWrapper名称= 3

[英]Inexplicable ResourceWarning: unclosed file <_io.TextIOWrapper name=3

I am finalising moving my code from python2.7 to python3.5 and turned on warnings to check another module. 我正在完成将代码从python2.7移至python3.5的操作,并打开warnings以检查另一个模块。
When using os.popen() I am getting the following error. 使用os.popen()时,出现以下错误。

ResourceWarning: unclosed file <_io.TextIOWrapper name=3 encoding='UTF-8'>

The number in the above example "name=3" will change, depending on the code but it is always a integer. 上面的示例中的“ name = 3”中的数字将有所变化,具体取决于代码,但是它始终是整数。
This code snippet produces the error and yet no file has been opened, which is in complete conflict with the error message unclosed file . 此代码段产生错误,但尚未打开任何文件,这与错误消息unclosed file完全冲突。
My environment is Linux using python 3.5.2 我的环境是使用python 3.5.2的Linux

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import warnings
import os
warnings.simplefilter('default')
sink_list = os.popen('pacmd list-sinks | grep "name:" | cut --delimiter=: -f2').readlines()
print (sink_list)
sink = os.popen('pacmd list | grep "Default sink name" | cut --delimiter=: -f2').readline()
print(sink)

Results in the following: 结果如下:

test.py:6: ResourceWarning: unclosed file <_io.TextIOWrapper name=3 encoding='UTF-8'>
  sink_list = os.popen('pacmd list-sinks | grep "name:" | cut --delimiter=: -f2').readlines()
[' <alsa_output.pci-0000_00_1b.0.analog-stereo>\n', ' <fs2-Equaliser>\n', ' <fs2-bs2b>\n']
test.py:8: ResourceWarning: unclosed file <_io.TextIOWrapper name=3 encoding='UTF-8'>
  sink = os.popen('pacmd list | grep "Default sink name" | cut --delimiter=: -f2').readline()
 fs2-Equaliser

Does anyone know why this warning is issued, especially in this circumstance, where no file has been opened? 有谁知道为什么发出此警告,尤其是在这种情况下,没有打开文件?

The integer is a file descriptor , the integer number the OS uses to talk about file handles assigned to a process. 整数是文件描述符 ,操作系统用来谈论分配给进程的文件句柄的整数。 0 , 1 and 2 are stdin , stdout and stderr , 3 and up are further file descriptors used. 012stdinstdoutstderr ,3个和至多是用于进一步文件描述符。

You get the resource warning because you open the file handle, but never close it explicitly. 之所以收到资源警告,是因为您打开了文件句柄,但从未明确关闭它。 You instead just call .readlines() or .readline() on the Python file object wrapper: 相反,您只需要在Python文件对象包装器上调用.readlines().readline()

sink_list = os.popen('pacmd list-sinks | grep "name:" | cut --delimiter=: -f2').readlines()

This leaves the file object to be closed by the garbage collector, and you get the warning. 这使文件对象被垃圾收集器关闭,您将收到警告。 You can use the open object as a context manager to have it closed for you: 您可以使用打开的对象作为上下文管理器来为您关闭它:

 with os.popen('pacmd list-sinks | grep "name:" | cut --delimiter=: -f2') as list_sinks:
    sink_list = list_sinks.readlines()

Personally, I'd use the subprocess module to handle external processes, and use Python to do the line selection. 就个人而言,我将使用subprocess模块来处理外部流程,并使用Python进行行选择。 This lets you avoid spinning up a separate sh process and generally has much nicer exception handling: 这样可以避免启动单独的sh进程,并且通常具有更好的异常处理:

import subprocess

# read list of sinks
result = suprocess.run(['pacmd', 'list-sinks'], stdout=subprocess.STDOUT, encoding='UTF-8')
sink_list = [l.split(':', 2) for l in result.stdout if 'name:' in l]

# read default sink
result = suprocess.run(['pacmd', 'list'], stdout=subprocess.STDOUT, encoding='UTF-8')
default_sink = next((l.split(':', 2) for l in result.stdout if 'Default sink name' in l), None)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 3:ResourceWarning:unclosed文件<_io.TextIOWrapper name ='PATH_OF_FILE' - Python 3: ResourceWarning: unclosed file <_io.TextIOWrapper name='PATH_OF_FILE' ResourceWarning:未关闭的文件 &lt;_io.BufferedReader name=4&gt; - ResourceWarning: unclosed file <_io.BufferedReader name=4> Python3:Reportlab图像 - ResourceWarning:未闭合文件&lt;_io.BufferedReader name = ...&gt; - Python3: Reportlab Image - ResourceWarning: unclosed file <_io.BufferedReader name=…> glob error &lt;_io.TextIOWrapper name =&#39;...&#39;mode =&#39;r&#39;scoding =&#39;cp1252&#39;&gt;读取文本文件错误 - glob error <_io.TextIOWrapper name='…' mode='r' encoding='cp1252'> reading text file error 如何从文件中删除&lt;_io.TextIOWrapper name =&#39;xyz.txt&#39;mode =&#39;w&#39;encoding =&#39;UTF-8&#39;&gt;? - How to remove <_io.TextIOWrapper name='xyz.txt' mode='w' encoding='UTF-8'> from file? 将信息添加到文本文件,TypeError:&#39;_io.TextIOWrapper&#39;对象不可下标 - Adding information to a text file, TypeError: '_io.TextIOWrapper' object is not subscriptable AttributeError: &#39;_io.TextIOWrapper&#39; 对象没有 txt 文件的属性 &#39;lower&#39; - AttributeError: '_io.TextIOWrapper' object has no attribute 'lower' for txt file 如何使用 name 属性实例化 io.TextIOWrapper 对象? - How to instantiate an io.TextIOWrapper object with a name attribute? &#39;_io.TextIOWrapper&#39;没有属性&#39;replace&#39; - '_io.TextIOWrapper' no attribute 'replace' _io.TextIOWrapper&#39;对象不可调用 - _io.TextIOWrapper' object is not callable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM