Skip to content

Instantly share code, notes, and snippets.

@antunesleo
Created August 23, 2018 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antunesleo/d5848d6d7dfa1f317f734a192f69a0a4 to your computer and use it in GitHub Desktop.
Save antunesleo/d5848d6d7dfa1f317f734a192f69a0a4 to your computer and use it in GitHub Desktop.
Not tested
import os
import shutil
from distutils import dir_util
class DirAlreadyExists(Exception):
pass
class FileAlreadyExists(Exception):
pass
class FilesAndDirsService(object)
@classmethod
def create_a_dir(cls, path, dir_name):
dir_to_create = '{}/{}'.format(path, dir_name)
if os.path.exists(dir_to_create):
raise DirAlreadyExists('Could not create the dir {} because already exists'.format(dir_to_create))
os.makedirs(dir_to_create)
@classmethod
def move_a_file(cls, path, new_path, file_name)
current_file_path = '{}/{}'.format(path, file_name)
new_file_path = '{}/{}'.format(new_path, file_name)
if os.path.isfile(new_file_path):
raise FileAlreadyExists('Could not create the file {} because already exists'.format(new_file_path))
os.rename(current_file_path, new_file_path)
@classmethod
def move_a_dir(cls, path, new_path, dir_name)
current_dir_path = '{}/{}'.format(path, dir_name)
new_dir_path = '{}/{}'.format(new_path, dir_name)
if os.path.exists(new_dir_path):
raise DirAlreadyExists('Could not create the dir {} because already exists'.format(new_dir_path))
os.rename(dir_path, new_dir_path)
@classmethod
def copy_a_file(cls, path, copy_path, file_name)
file_path = '{}/{}'.format(path, file_name)
copy_file_path = '{}/{}'.format(copy_path, file_name)
if os.path.isfile(copy_file_path):
raise FileAlreadyExists('Could not create the file {} because already exists'.format(copy_file_path))
shutil.copyfile(file_path, copy_file_path)
@classmethod
def copy_a_dir(cls, path, new_path, dir_name)
current_dir_path = '{}/{}'.format(path, dir_name)
copy_dir_path = '{}/{}'.format(copy_path, dir_name)
if os.path.exists(copy_dir_path):
raise DirAlreadyExists('Could not create the dir {} because already exists'.format(copy_dir_path))
os.makedirs(copy_dir_path)
dir_util.copy_tree(current_dir_path, copy_dir_path)
@classmethod
def remove_a_file(cls, path, file_name)
file_path = '{}/{}'.format(path, file_name)
os.remove(file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment