How to redirect output with subprocess in Python?

后端 未结 5 1618
孤街浪徒
孤街浪徒 2020-11-22 07:41

What I do in the command line:

cat file1 file2 file3 > myfile

What I want to do with python:

import subprocess, shlex
my         


        
5条回答
  •  迷失自我
    2020-11-22 08:20

    size = 'ffprobe -v error -show_entries format=size -of default=noprint_wrappers=1:nokey=1 dump.mp4 > file'
    proc = subprocess.Popen(shlex.split(size), shell=True)
    time.sleep(1)
    proc.terminate() #proc.kill() modify it by a suggestion
    size = ""
    with open('file', 'r') as infile:
        for line in infile.readlines():
            size += line.strip()
    
    print(size)
    os.remove('file')
    

    When you use subprocess , the process must be killed.This is an example.If you don't kill the process , file will be empty and you can read nothing.It can run on Windows.I can`t make sure that it can run on Unix.

提交回复
热议问题