I am trying to automate a program in Python that runs in terminal on Ubuntu.
I have a lot of files in my directory, and each file has a sister file associated with it, all in the same directory. The names of the files start with 1 and go all the way up to 500.
For example, there will be files like 1.mp3, 1_hello.mp4, 1_something.mp4, 1_something_else.mp3 and 2.mp3, 2_hello.mp4, 2_something.mp4, 2_something_else.mp3.
I will be only concerned with 1.mp3, and 1_hello.mp4, and similarly for all the other files in my directory. Other files with the same suffix don’t matter to me. How can I automate this? I have tried this but, it doesn’t work.
import os
directory_name = '/home/user/folder/'
for file_name in os.listdir(directory_name):
if file_name.endswith(".mp3"):
for sis_file in os.listdir(directory_name):
if sis_file.endswith("file_name._hello.mp4"):
os.system("command file_name -a file_name.sis_file file_name.new_file")
The command is written in the os.system line. new_file is created as the result of this operation, and it too must have the suffix of the original file for it to be easily identifiable. Also, for the command in os.system, each file must only be paired only with its sister file, or I will get inconsistent results.
Edit: I will a lot of files in my directory. All the files are sequentially numbered, begining from 1. Each file has many other files associated with it, and all these sister files will have same prefix as that of the parent file. The command that I have to automate is like this
command name_of_the_parent_file.mp3 -a name_of_the_sister_file.txt name_of_the_output_file.mp4
name_of_the_parent_file would be like 1.mp3, 2.mp3, 3.mp3
name_of_the_sister_file would be like 1_hello.txt, 2_hello.txt
name_of_the_output_file would be the name of the new file that this command creates.
2
Answers
this is what I gathered from your question.
the output is separating all the numbered files in the folder:
If I get you right you need to run this command on all parent files combined with all paired children files individually: