subprocess.call()在将标准输出重定向到文件时抛出错误“ FileNotFoundError:[Errno 2]没有这样的文件或目录”

问题描述 投票:0回答:1

我想将控制台输出重定向到文本文件以进行进一步检查。任务是从栅格文件(TIFF)中提取TIFF-TAG并过滤结果。为了实现这一点,我手头有几个工具。其中一些不是python库,而是命令行工具,例如ImageMagick的“ identify”。

我传递给subprocess.check_call()的示例命令字符串为:

cmd_str = 'identify -verbose /home/andylu/Desktop/Models_Master/AERSURFACE/Input/Images/Denia_CORINE_CODE_18_reclass_NLCD92_reproj_ADAPTED_Europe_AEA.tif | grep -i "274"'

在此处,通过“标识”产生的TIFF-TAG的输出中,包含有关TAG号“ 274”的信息的所有行都应显示在控制台中,或写入文件中。

错误类型1:在控制台中显示

subprocess.check_call(bash_str, shell=True)

subprocess.CalledProcessError: Command 'identify -verbose /home/andylu/Desktop/Models_Master/AERSURFACE/Input/Images/Denia_CORINE_CODE_18_reclass_NLCD92_reproj_ADAPTED_Europe_AEA.tif | grep -i "274"' returned non-zero exit status 1.

错误类型2:将输出重定向到文本文件

subprocess.call(bash_str, stdout=filehandle_dummy, stderr=filehandle_dummy

FileNotFoundError: [Errno 2] No such file or directory: 'identify -verbose /home/andylu/Desktop/Models_Master/AERSURFACE/Input/Images/Denia_CORINE_CODE_18_reclass_NLCD92_reproj_ADAPTED_Europe_AEA.tif | grep -i "274"': 'identify -verbose /home/andylu/Desktop/Models_Master/AERSURFACE/Input/Images/Denia_CORINE_CODE_18_reclass_NLCD92_reproj_ADAPTED_Europe_AEA.tif | grep -i "274"'

CODE

这些subprocess.check_call()函数由以下便利函数执行:

def subprocess_stdout_to_console_or_file(bash_str, filehandle=None):
    """Function documentation:\n
    Convenience tool which either prints out directly in the provided shell, i.e. console,
    or redirects the output to a given file.

    NOTE on file redirection: it must not be the filepath, but the FILEHANDLE,
    which can be achieved via the open(filepath, "w")-function, e.g. like so:
    filehandle = open('out.txt', 'w')
    print(filehandle): <_io.TextIOWrapper name='bla_dummy.txt' mode='w' encoding='UTF-8'>
    """

    # Check whether a filehandle has been passed or not
    if filehandle is None:
        # i) If not, just direct the output to the BASH (shell), i.e. the console
        subprocess.check_call(bash_str, shell=True)
    else:
        # ii) Otherwise, write to the provided file via its filehandle
        subprocess.check_call(bash_str, stdout=filehandle)

发生一切的代码段已经将print()的输出重定向到文本文件。在函数print_out_all_TIFF_Tags_n_filter_for_desired_TAGs()中调用上述函数。

由于子进程的输出不会与print()的输出一起自动重定向,因此有必要通过其关键字参数“ stdout”将文件句柄传递给subprocess.check_call(bash_str, stdout=filehandle)。尽管如此,在由contextlib.redirect_stdout()创建的stdout的重定向区域之外,也会发生上述错误。

dummy_filename = "/home/andylu/bla_dummy.txt"  # will be saved temporarily in the user's home folder

# NOTE on scope: redirect sys.stdout for python 3.4x according to the following website_
# https://stackoverflow.com/questions/14197009/how-can-i-redirect-print-output-of-a-function-in-python
with open(dummy_filename, 'w') as f:
    with contextlib.redirect_stdout(f):
        print_out_all_TIFF_Tags_n_filter_for_desired_TAGs(
            TIFF_filepath)

OS和Python版本

  • OS:

    NAME =“ Ubuntu”VERSION =“ 18.04.3 LTS(Bionic Beaver)”ID = ubuntuID_LIKE = debianPRETTY_NAME =“ Ubuntu 18.04.3 LTS”VERSION_ID =“ 18.04”

  • Python:

  • Python 3.7.6(默认值,2020年1月8日,19:59:22)[GCC 7.3.0] :: Linux上的Anaconda,Inc。

我想将控制台输出重定向到文本文件以进行进一步检查。任务是从栅格文件(TIFF)中提取TIFF-TAG并过滤结果。为了实现这一点,我有几个...

python subprocess stdout
1个回答
0
投票

我曾经遇到过类似的问题,并且已经解决了。

© www.soinside.com 2019 - 2024. All rights reserved.