Skip to content

Instantly share code, notes, and snippets.

@poacosta
Last active January 16, 2023 03:31
Show Gist options
  • Select an option

  • Save poacosta/f9a19b7b51f692421dec856e1050422b to your computer and use it in GitHub Desktop.

Select an option

Save poacosta/f9a19b7b51f692421dec856e1050422b to your computer and use it in GitHub Desktop.
Create directories by file names given a path using Python
import os
def create_dirs_by_filenames(filenames, parent_dir):
mode = 0o777
for directory in filenames:
os.mkdir(os.path.join(parent_dir, directory), mode)
def get_filenames(path):
dir_list = [os.path.splitext(x)[0].replace(':', '-') for x in os.listdir(path)]
return sorted(set(dir_list))
if __name__ == '__main__':
path = "/<desired-path>/"
create_dirs_by_filenames(get_filenames(path), path)
print("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment