Phoning a friend - For loop problem

Greetings all,

Ok. I’m admittedly stretching my limited python skills here but, I had an opportunity to try and solve a work related problem with some code and have been giving it the old college try using what I’ve learned so far. I can’t seem to formulate the “for loop” in the “encode_all” function below correctly tho. Here’s what I’ve done so far:

import subprocess, sys, os

def encode_one(in_file, out_file): # This works as expected
process = "ffmpeg -i {} {} ".format(in_file, out_file)
subprocess.run(process, shell=True)

def encode_all(): # This doesn’t work as expected
print(" >>> Entering function: ") # debug check
cwd = os.getcwd
print("cwd = %s " % os.getcwd()) # debug check
in_file = input("Extension of input files: ")
out_file = input("Extensionof desired output file: ")
process = "ffmpeg -i {} {} “.format(in_file, out_file)
for i in in_file:
print(”>>> The in_file = “, in_file)
if i.endswith((in_file)):
print(” i = “, i)
subprocess.Popen(process, shell=True)
print(” <<<< Exiting function ") # debug check

Long story short.

I’ve got a folder with 10 .MOV files in it that I want to batch process using ffmpeg. For the life of me though, I can’t get it to iterate through all the files in the directory and process them based on the .MOV file extension. I’ve done some trouble shooting and debugging like Zed taught in the text and it gets to the “for loop” and outputs the following:

Entering function:
cwd = /home/pi/disk1/test
Extension of input files: .MOV
Extension of desired output files: .mp4

The in_file = .MOV
The in_file = .MOV
The in_file = .MOV
The in_file = .MOV

There are 10 files in the current working directory and I can’t wrap my mind around this output. If it was iterating through every file in the directory, I would think I would see 10 similar output statements. Not four. I know the working directory is correct, because it’s listed correctly in the output.

If anyone has an insights, I would greatly appreciate it. I’m stumped at the moment.

Thanks!

Ryan

in_file is a string, with no connection whatsoever to any files. When you loop over a string, the loop variable just traverses the characters one by one: i == '.', then M, O, V.

You are totally leaving out the step where you glob *.mov in the file system.

Ok, duly noted sir…but, I’d be lying if I said I knew what a “glob” was. I had surmised that it was seeing .mov as a string, but I didn’t know how to rectify that. I’ll research “glob” and see where that takes me.

Thx!

Ryan

Oh, sorry, glob is just a bash utility to search the file system for entries that match a given pattern.
I recommend that you take a look at the pathlib library.

By the way, it’s great that you’re using the opportunity to learn some Python, but it’s not really the best tool for the job. This is like a one liner in bash or Powershell.

1 Like

It’s cool that you are having a go at a real world problem though. Keep going!

Oh, sorry, glob is just a bash utility to search the file system for entries that match a given pattern. I recommend that you take a look at the pathlib library.

Got it…will do. Thx again.

This is like a one liner in bash or Powershell.

#truth and I’ve been able to accomplish this task in each but, it was an opportunity to try and reinforce and/or hammer home some of the things I learned thus far. Plus, I learned a some new things I didn’t know…score.

@gpkesley It’s cool that you are having a go at a real world problem though. Keep going!

Thx! I will…it’s gotten under my skin a little because I feel like I’m almost there.

1 Like

Started googling “glob” the other night at the start of this tread. I came across How to use Glob() function to find files recursively in Python?
Started playing with it in python and I’ll defiantly will keep it on hand for working with files…especially after I figure out all how it works lol

A free service run by Zed A. Shaw for learncodethehardway.org.