Skip to content

Instantly share code, notes, and snippets.

@ox1111
Last active February 16, 2023 22:44
Show Gist options
  • Save ox1111/0ccd991c300b6ce303259f66de1a5a21 to your computer and use it in GitHub Desktop.
Save ox1111/0ccd991c300b6ce303259f66de1a5a21 to your computer and use it in GitHub Desktop.
make directory
# -*- coding: utf-8 -*-
#
# write by kyoung chip , jang
#
# python 3.6
#
# ubuntu : python3 directory_maker.py test333/test1212
# windows : python3 directory_maker.py test111\test777
#
import os
import sys
class DirectoryMaker:
def make_dir(self, path):
pass
class WindowsDirectoryMaker(DirectoryMaker):
def make_dir(self, path):
path_components = path.split("\\")
root_dir = path_components[0] + "\\"
sub_dirs = path_components[1:]
for i in range(len(sub_dirs)):
if not os.path.exists(root_dir):
os.mkdir(root_dir)
root_dir = os.path.join(root_dir, sub_dirs[i])
if not os.path.exists(root_dir):
os.mkdir(root_dir)
class LinuxDirectoryMaker(DirectoryMaker):
def make_dir(self, path):
path_components = path.split("/")
root_dir = path_components[0] + "/"
sub_dirs = path_components[1:]
for i in range(len(sub_dirs)):
if not os.path.exists(root_dir):
os.mkdir(root_dir)
root_dir = os.path.join(root_dir, sub_dirs[i])
if not os.path.exists(root_dir):
os.mkdir(root_dir)
class DirectoryMakerFactory:
def __init__(self):
pass
def get_directory_maker(self):
if os.name == "nt":
return WindowsDirectoryMaker()
else:
return LinuxDirectoryMaker()
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python directory_maker.py <path>")
sys.exit(1)
path = sys.argv[1]
directory_maker_factory = DirectoryMakerFactory()
directory_maker = directory_maker_factory.get_directory_maker()
directory_maker.make_dir(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment