how can I make directory?

Question:

The given code is showing me the error;
I am making the folder under folder under folder according to different classes and its videos. train is folder contains different class folder, every class folder have certain videos. directory of train is something like this;

---train

  ----class1

         ----video11.mp4
         ----video12.mp4
             ...
  ----class2
         ----video21.mp4
         ----video22.mp4
             ...
      ...

I want to create folders of above directory like this;

---train_data

  ----class1
         ----video11
         ----video12
             ...
  ----class2
         ----video21
         ----video22
             ...
      ...
    

I have written this code, but this is showing error, I don’t know why os.mkdir is not working here

import cv2
import os
from tqdm import tqdm

for data_dir, dest_dir in ["train", 'train_data'],["val", 'val_data']:
    for classes in tqdm(os.listdir(data_dir)):
        for videos in os.listdir(data_dir + '/' + classes):
            print(videos)
            print(classes)
            print(dest_dir)
            if not os.path.exists(dest_dir + '/' + classes + '/' + videos):
                os.mkdir(dest_dir + '/' + classes + '/' + videos)

Error:

v_ApplyEyeMakeup_g01_c01.avi
ApplyEyeMakeup
train_data
  0%|          | 0/101 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "F:/Research/Action Recognition/Mine/data.py", line 12, in <module>
    os.mkdir(dest_dir + '/' + classes + '/' + videos)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'train_data/ApplyEyeMakeup/v_ApplyEyeMakeup_g01_c01.avi'

My directory is;

enter image description here

Asked By: Tariq Hussain

||

Answers:

You can use pathlib module from standard library:

import pathlib
import itertools

extensions = ['mp4', 'avi', 'mkv']

videos = itertools.chain.from_iterable([root_dir.glob(f'**/*.{ext}')
                                            for ext in extensions])

for video in videos:
    (video.parent / video.stem).mkdir(exist_ok=True)
Answered By: Corralien

You can use makedir module from the OS standard library:

os.makedir() 

instead of

os.mkdir()

Because the os has changed some of its commands.

Answered By: Tariq Hussain Dahri

os has changed some of its commands.

Use;

os.makedirs()

instead

   os.makedir()

or you can use mkdir() as well