0

I have this code:

outtxt = "output2.txt"
command = f"ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 {inputpath}"
with open(outtxt, "w") as output:
    subprocess.Popen(command, stdout=output, stderr=output, text=True, creationflags=0x08000000)
    output.close()

with open(outtxt, "r") as file:
    data = file.read()
print(data)

When I run this, Python only prints a blank line. ("") But there is definitely a file like that and it has a single line content that stores a video file's frame count. 813 But python can't read that line. How can I read the frame count and convert to an integer? (I am on Windows 10)

Edit: Sorry for indentation. I fixed the indentation and wrote my full code. Also, i tried to close the first file but that also didn't work. I checked output2.txt and it is not empty. I deleted and tried again and it was not empty.

Nande
  • 5
  • 3

1 Answers1

1

Popen() merely starts a process. You will need to .communicate() and/or .wait() for it ... or, much better, use subprocess.run() which takes care of these minutiae on your behalf.

outtxt = "output2.txt"
command = f"ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 {inputpath}"
with open(outtxt, "w") as output:
    subprocess.run(command, stdout=output, stderr=output, text=True, creationflags=0x08000000)

As noted in comments already, the with statement takes care of closing the file for you, so there is no need to explicitly .close() it.

tripleee
  • 175,061
  • 34
  • 275
  • 318