Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 它不打印输出,但为什么不在文件中写入?_Python_Subprocess - Fatal编程技术网

Python 它不打印输出,但为什么不在文件中写入?

Python 它不打印输出,但为什么不在文件中写入?,python,subprocess,Python,Subprocess,在for循环完成后,您需要在写入文件后关闭该文件 用于execute(['ls','-ltr']): f、 写(用) f、 关闭() 这在尝试中更安全finally阻塞,因为即使对文件执行操作时出错,它也会关闭文件 试试看: f=开放(“sum.txt”、“a”) 用于execute(['ls','-ltr']): f、 写(用) 最后: f、 关闭() 或者更好地使用with语句,因为这将在语句执行后关闭文件 打开(“sum.txt”,“a”)作为f: 用于execute(['ls','-

for
循环完成后,您需要在写入文件后关闭该文件

用于execute(['ls','-ltr']):
f、 写(用)
f、 关闭()
这在
尝试中更安全
finally
阻塞,因为即使对文件执行操作时出错,它也会关闭文件

试试看:
f=开放(“sum.txt”、“a”)
用于execute(['ls','-ltr']):
f、 写(用)
最后:
f、 关闭()
或者更好地使用
with
语句,因为这将在语句执行后关闭文件

打开(“sum.txt”,“a”)作为f:
用于execute(['ls','-ltr']):
f、 写(用)

for
循环完成后,您需要在写入文件后关闭该文件

用于execute(['ls','-ltr']):
f、 写(用)
f、 关闭()
这在
尝试中更安全
finally
阻塞,因为即使对文件执行操作时出错,它也会关闭文件

试试看:
f=开放(“sum.txt”、“a”)
用于execute(['ls','-ltr']):
f、 写(用)
最后:
f、 关闭()
或者更好地使用
with
语句,因为这将在语句执行后关闭文件

打开(“sum.txt”,“a”)作为f:
用于execute(['ls','-ltr']):
f、 写(用)

您应该使用上下文管理器打开文件。使用此解决方案,您的文件将被关闭

代码:

import subprocess
f = open("sum.txt","a")
def execute(cmd):
        popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
        for stdout_line in iter(popen.stdout.readline, ""):
            yield stdout_line 
        popen.stdout.close()
        return_code = popen.wait()
        if return_code:
            raise subprocess.CalledProcessError(return_code, cmd)




for uses in execute(['ls','-ltr']):
    f.write(uses)
    print(uses)
import subprocess


def execute(cmd):
        popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
        for stdout_line in iter(popen.stdout.readline, ""):
            yield stdout_line
        popen.stdout.close()
        return_code = popen.wait()
        if return_code:
            raise subprocess.CalledProcessError(return_code, cmd)


with open("sum.txt", "a") as opened_file:
    for uses in execute(['ls', '-ltr']):
        opened_file.write(uses)
        print(uses)
输出:

import subprocess
f = open("sum.txt","a")
def execute(cmd):
        popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
        for stdout_line in iter(popen.stdout.readline, ""):
            yield stdout_line 
        popen.stdout.close()
        return_code = popen.wait()
        if return_code:
            raise subprocess.CalledProcessError(return_code, cmd)




for uses in execute(['ls','-ltr']):
    f.write(uses)
    print(uses)
import subprocess


def execute(cmd):
        popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
        for stdout_line in iter(popen.stdout.readline, ""):
            yield stdout_line
        popen.stdout.close()
        return_code = popen.wait()
        if return_code:
            raise subprocess.CalledProcessError(return_code, cmd)


with open("sum.txt", "a") as opened_file:
    for uses in execute(['ls', '-ltr']):
        opened_file.write(uses)
        print(uses)

sum.txt文件的内容相同。

您应该使用上下文管理器打开文件。使用此解决方案,您的文件将被关闭

代码:

import subprocess
f = open("sum.txt","a")
def execute(cmd):
        popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
        for stdout_line in iter(popen.stdout.readline, ""):
            yield stdout_line 
        popen.stdout.close()
        return_code = popen.wait()
        if return_code:
            raise subprocess.CalledProcessError(return_code, cmd)




for uses in execute(['ls','-ltr']):
    f.write(uses)
    print(uses)
import subprocess


def execute(cmd):
        popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
        for stdout_line in iter(popen.stdout.readline, ""):
            yield stdout_line
        popen.stdout.close()
        return_code = popen.wait()
        if return_code:
            raise subprocess.CalledProcessError(return_code, cmd)


with open("sum.txt", "a") as opened_file:
    for uses in execute(['ls', '-ltr']):
        opened_file.write(uses)
        print(uses)
输出:

import subprocess
f = open("sum.txt","a")
def execute(cmd):
        popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
        for stdout_line in iter(popen.stdout.readline, ""):
            yield stdout_line 
        popen.stdout.close()
        return_code = popen.wait()
        if return_code:
            raise subprocess.CalledProcessError(return_code, cmd)




for uses in execute(['ls','-ltr']):
    f.write(uses)
    print(uses)
import subprocess


def execute(cmd):
        popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
        for stdout_line in iter(popen.stdout.readline, ""):
            yield stdout_line
        popen.stdout.close()
        return_code = popen.wait()
        if return_code:
            raise subprocess.CalledProcessError(return_code, cmd)


with open("sum.txt", "a") as opened_file:
    for uses in execute(['ls', '-ltr']):
        opened_file.write(uses)
        print(uses)

sum.txt文件的内容相同。

要避免这些类型的错误,请在代码中使用
和open()
。它将自动关闭文件。

要避免此类错误,请在代码中使用
和open()
。它将自动关闭文件。

您应该在完成后关闭文件。是否关闭文件?在关闭文件而不是列出文件后,它会给出总数并写入总数file@SumitDuwal,您能否显示更新的代码和输出?总共36次回溯(最近一次调用):文件“”,第3行,in-ValueError:关闭文件上的I/O操作完成后,您应该
关闭
文件是否关闭文件?关闭文件而不是列出文件后,它会给出总数并写入总数file@SumitDuwal,您能否显示更新的代码和输出?总共36次回溯(最近一次调用):文件“”,第3行,in-ValueError:对关闭的文件执行I/O操作