I have a list of file names but without their absolute path. I am trying to write a script which will read the file line by line and search the files recursively in the given directory. When the file is found, it will move it to another directory. This script will run on a Linux based machine and I know how I can do it using find
and I can eventually use subprocess
but I was wondering if there is more Pythonic way to do it.
This is a short excerpt from the file containing the file names:
Florida'sUltimatePilots_Ep03.mp4
MonsterFish_2016_EP8.srt
MonsterFish_2016_EP9.srt
MonsterFish_2016_EP10.srt
Chronicles_2018_EP1.mp4
Pilot Fever 2012.mp4
And this is my Python script so far:
import os
import sys
import time
import shutil
file_list = "/home/user/test/test/files_test.txt"
input_directory = "/home/user/test/in"
output_directory = "/home/user/test/out"
srt_directory = os.path.join(output_directory, "srt")
mp4_directory = os.path.join(output_directory, "mp4")
def find_abspath(filename, input_directory):
.......
return filename_abspath
with open(file_list) as flist:
for filename in flist:
if ".mp4" in filename:
# print(f"MP4: {filename}")
filename_abspath = find_abspath(filename, input_directory)
shutil.move(filename_abspath, mp4_directory)
time.sleep(60)
elif ".srt" in filename:
# print(f"SRT: {filename}")
filename_abspath = find_abspath(filename, input_directory)
shutil.move(filename_abspath, srt_directory)
So I only need some help with the find_abspath
function. The rest I can figure out on my own. The problem is that on this machine my user doesn't have admin rights and it is not connected to the Internet, so I don't want to install any third-party libraries for Python. As a backup plan, I can try to write a BASH
script but probably it will take me slightly more time.