1

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.

Georgi Stoyanov
  • 594
  • 1
  • 9
  • 26
  • 1
    I suggest you take a look at os.path.walk() I believe this function does exactly what you are looking for. – RomainL. Sep 05 '19 at 09:07
  • 1
    Maybe this SO question can point you in the right direction: https://stackoverflow.com/questions/2186525/how-to-use-glob-to-find-files-recursively – ledwinder96 Sep 05 '19 at 09:10

1 Answers1

2

I don't understand your exact requirement but hopefully, this might be helpful

def find_abspath(filename, input_directory):
    filename = filename.rstrip('\n')
    for dir_name, subdir_list, file_list in os.walk(input_directory):
        if filename in file_list:
            filename_abspath = os.path.join(dir_name, filename)
    return filename_abspath
Prateek Sen
  • 392
  • 2
  • 9
  • Also take a look at the function os.path.abspath to normalise the absolute path – Svavelsyra Sep 05 '19 at 09:24
  • @PrateekSen it actually doesn't work, I think you need to have an additional `if` statement to check if the filename is in the `file_list` but this statement also doesn't work, so I am trying to find a way to make it work. – Georgi Stoyanov Sep 05 '19 at 09:52
  • Apparently the `filename` and the file in the `list_files` are different even though they look exactly the same to me. – Georgi Stoyanov Sep 05 '19 at 10:12
  • found the problem, I needed to strip the new line from the name of the file like this: `filename = filename.rstrip('\n')` And then I needed to add an if statement like this: `if filename in files:` before joining the `dir_name` and the `filename`. – Georgi Stoyanov Sep 05 '19 at 11:28
  • I have updated the code snippet, you can check now, I hope it will work for your case. – Prateek Sen Sep 05 '19 at 12:45