0

Hello I am trying to create a subdirectory called eeg within another subdirectory in the following way:

for idx in range(0,5):
    path = '/Users/fred/Dropbox/1_Mon_Switch/Statistiques/Python/RenameFileBids/Raw/'
    dest_dir = "{}{}{:02d}{}".format(path,'sub-',idx,'/eeg/')
    os.mkdir(dest_dir)

When I run this code I have the following error message : FileNotFoundError: [Errno 2] No such file or directory: '/Users/fred/Dropbox/1_Mon_Switch/Statistiques/Python/RenameFileBids/Raw/sub-00/eeg/'

This code is running fine when I remove the second subfolder /eeg/ the sub-00 directory is created. Could you tell me where I'm wrong? Thank you for your help

Yevhen Kuzmovych
  • 5,476
  • 3
  • 19
  • 36
  • Use [`os.makedirs()`](https://docs.python.org/3/library/os.html#os.makedirs) to create multiple elements at once. `os.mkdir` requires the parent directory to already exist. – Charles Duffy Mar 04 '21 at 16:22
  • Thank you for the tips. Why do you recommend to remove the trailing / ? – Frédéric Ooms Mar 04 '21 at 16:26
  • Code doing a `split('/')` or equivalent may or may not assume a trailing slash (thus, an empty group at the end) to be valid -- basically, it's more work you're assuming anything you call through will be doing right. Might not cause a problem, but it's generally better to just avoid making work for other components (and thus depending on them being written correctly). Same for `//`s in names -- supposed to just be ignored except at the first position (where it's legal for the filesystem to treat it specially), but sometimes code is buggy and _doesn't_ ignore it. – Charles Duffy Mar 04 '21 at 16:28

0 Answers0