Mahdi Rayeni
Mahdi Rayeni

Reputation: 1

Python giving FileNotFoundError for file name

I was trying to iterate over the files in a directory like this:

with open(video_list, 'r') as imf:
    index = []
    for id, line in enumerate(imf):

        video_label = line.strip().split()

        video_name = video_label[0]  # name of video
        label = rectify_label[video_label[1]]  # label of video

        video_path = os.path.join(video_root, video_name)  # video_path is the path of each video
        ###  for sampling triple imgs in the single video_path  ####

        img_lists = os.listdir(video_path)
        img_lists.sort()  # sort files by ascending
        img_count = len(img_lists)  # number of frames in video
        num_per_part = int(img_count) // 3

But Python was throwing FileNotFoundError even though the file exists:

---> 36             img_lists = os.listdir(video_path)
 37             img_lists.sort()  # sort files by ascending
 38             img_count = len(img_lists)  # number of frames in video

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:/Users/m_rayeni/Emotion-FAN/Data/Train/Fear/013736360'

So what is wrong here?

Upvotes: 0

Views: 58

Answers (1)

anomanderrake
anomanderrake

Reputation: 61

I think the error here is that you are using forward Slash in the File name instead of back slash. Your current path is "C:/Users/m_rayeni/Emotion-FAN/Data/Train/Fear/013736360" It should be "C:\Users\m_rayeni\Emotion-FAN\Data\Train\Fear\013736360"

In all likeliness this won't work either and you would need to escape the backslash like so "C:\\Users\\m_rayeni\\Emotion-FAN\\Data\\Train\\Fear\\013736360"

Or use raw stings like - r"C:\Users\m_rayeni\Emotion-FAN\Data\Train\Fear\013736360"

Upvotes: 1

Related Questions