Why can't my file be found in my directory?

251 views Asked by At

I'm trying to use the openai-whisper library to transcribe text from a video in the same folder as my program. However, I keep getting a FileNotFoundError: [WinError 2] The system cannot find the file specified

This is my code:

import whisper
import os

directory = os.path.dirname(os.path.abspath(__file__))
start = 0
videoList = []

for file in os.listdir(directory):
    if file.endswith('.mp4'):
        videoList.append(file)

model = whisper.load_model('base')

for video in videoList:
    start += 1
    result = model.transcribe(video)

    with open("Video " f'{start}' "transcription.txt", "w") as f:
        f.write(result["text"] + '\n')

I get the error when I reach the line where I am trying to transcribe the video.

I tried to see if there was a problem with finding the files in my directory at first which is why I have that first for loop. I tried seeing if my program wasn't finding the mp4 files but it is.

1

There are 1 answers

1
Grismar On BEST ANSWER

This is likely confusion around the idea of a working directory. Every program on any OS has a working directory. This is the directory that the application will assume relative paths are relative to. So, when a program opens test.txt, it opens that file in the working directory. test.txt is a relative path, because it does not specify the full absolute path to the file (like C:\Temp\test.txt).

The working directory can be the same as the directory where the program or script is, but it's not a given. In this piece of code:

directory = os.path.dirname(os.path.abspath(__file__))

... you determine the directory the script is in. But that does not have to be the working directory. For example, say your project looks like this:

\project_dir
  \src
    my_script.py
    video.mp4
  readme.txt

If you run python src/my_script.py from project_dir, directory will be /project_dir/src, but the script's working directory will be /project_dir.

As a result, this code:

    result = model.transcribe(video)

Will look for video.mp4 in the working directory, when you really want to look for it in the source directory.

Either do this:

    result = model.transcribe(os.path.join(directory, video))

Or make this change to the previously discussed line:

directory = os.getcwd()

If you do indeed want to only operate on videos in the working directory.

Of course, you need to make sure your videos are actually in the working directory in that case.