I am trying to iterate through a directory, do something to each of the files and then save a new file with a different name similar to the question here, but I am looking for a solution using pathlib. I am just not sure how to add the desired ending to the end of the file name
movie_dir = pathlib.Path("/movies")
save_dir = pathlib.Path("/corrected_movies")
for dir in movie_dir.iterdir():
for movie_path in dir.iterdir():
save_path = save_dir / movie_path # want to add _corrected.avi to end of file name
So, assuming your movie_path
paths have the correct extension already, you can do something like:
save_path = save_dir / move_path.with_stem(movie_path.stem + "_corrected")
If they don't have the same extension, then you can add:
save_path = save_dir / move_path.with_stem(movie_path.stem + "_corrected").with_suffix(".avi")
Also, as mentioned in the comments, be careful that movie_path
is going to be an absolute path! Do a dry run to see. If that is the case, and you could simply extract the .name
, so:
save_path = (
save_dir / move_path.with_stem(movie_path.stem + "_corrected").with_suffix(".avi").name
)
Although, given your situation and not having to keep the original extension, I think this can just be simplified to:
save_path = save_dir / (movie_path.stem + "_corrected.avi")