Skip to content

Instantly share code, notes, and snippets.

@imneonizer
Created August 21, 2020 12:36
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 imneonizer/badc4244a430ccc26722140c5a81d814 to your computer and use it in GitHub Desktop.
Save imneonizer/badc4244a430ccc26722140c5a81d814 to your computer and use it in GitHub Desktop.
import os
import glob
import shutil
import uuid
class TempFile:
def __init__(self, base_path, ext):
self.path = os.path.normpath("{base_path}/{rand}{ext}".format(
base_path=base_path,
rand=str(uuid.uuid4()),
ext=ext))
def cleanup(self):
os.remove(self.path)
class FileSystem:
def __init__(self):
pass
def tempfile(self, base_path="./", ext=".temp"):
""" create a temporary file path"""
return TempFile(base_path, ext)
def exists(self, path):
"""
checks if a path / list of paths exists or not
input:
- path: string / list of strings representing paths on disk
output: boolean / list of booleans represneting whether paths exists or not
"""
if isinstance(path, str):
# return True if path exists else False
return os.path.exists(path)
else:
return list(map(lambda x: self.exists(x), path))
def makedirs(self, path, exist_ok=True):
"""
create directory for given path / list of paths
input:
- path: string / list of strings representing paths to create
- exist_ok: boolean if set to True it won't raise any error if path already exists
output: string / list of strings representing created paths
"""
if isinstance(path, str):
try:
# return created file path
os.makedirs(path, exist_ok=exist_ok); return path
except OSError as e: return e
else:
# return list of created files path
return list(map(lambda x: self.makedirs(x, exist_ok), path))
def remove(self, path, not_exist_ok=True):
"""
delete file or directory for given path / list of paths
input:
- path: string / list of strings representing paths to delete
- not_exist_ok: boolean if set to True it wont raise any error if path doesn't exists
output: list / list of list representing paths of deleted files
"""
if isinstance(path, str):
if os.path.exists(path):
try:
# try to remove the file and
# return removed file path
os.remove(path)
return path
except OSError:
# if failed then try to
# remove the directory and
# return removed file path
shutil.rmtree(path)
return path
except Exception as e:
return e
if not_exist_ok:
return FileNotFoundError
raise FileNotFoundError
else:
# return list of removed files path
return list(map(lambda x: self.remove(x, not_exist_ok), path))
def clean(self, path, include_ext=[], exclude_ext=[]):
"""
delete everything inside a given directory / list of directories
input:
- path: string / list of strings representing paths of directories to clean
- include_ext: file extension / sub dir name to match for deleting from list of all files
- eg: include_ext=["logs", ".jpg"] or include_ext=".jpg"
- exclude_ext: file extension / sub dir name to match which will not be deleted in any case if matched
- eg: exclude_ext=[".jpg"] or exclude_ext=".jpg"
output: list / list of list representing paths of deleted files / sub directories
"""
# check whether single input or batch of inputs
if isinstance(path, str):
# convert extensions to list for conditional blocks
include_ext = [include_ext] if isinstance(include_ext, str) else include_ext
exclude_ext = [exclude_ext] if isinstance(exclude_ext, str) else exclude_ext
removed = []
if os.path.exists(path):
for f in glob.glob(os.path.normpath(path)+"/*"):
# split extension from file name
# if its a directory or doesn't have any extension
# then keep dir / filename as extension
ext = os.path.splitext(f)[1]
ext = ext if ext else os.path.basename(f)
# if extension in exclude list then don't remove
if ext in exclude_ext: continue
if include_ext:
# if inclusive list is defined then
# only remove defined extensions
# other wise remove all found files
if ext not in include_ext:
continue
# append in list of removed files
removed.append(self.remove(f))
# return list of removed files
return removed
else:
# return list of list of removed files
return list(map(lambda x: self.clean(x, include_ext=include_ext, exclude_ext=exclude_ext), path))
def basename(self, path):
""" extract file name from a file path """
if isinstance(path, str):
return os.path.basename(os.path.normpath(path))
else:
return list(map(lambda x: self.basename(x), path))
def dirname(self, path, absolute=False):
""" extract dirname from a file path """
if isinstance(path, str):
if absolute:
path = os.path.abspath(path)
dirname = os.path.dirname(os.path.normpath(path))
return dirname if dirname else os.path.dirname(os.path.realpath(path))
else:
return list(map(lambda x: self.dirname(x), path))
def abspath(self, path):
""" return absolute paths """
if isinstance(path, str):
return os.path.abspath(path)
else:
return list(map(lambda x: self.abspath(x), path))
def realpath(self, path):
""" return real paths """
if isinstance(path, str):
return os.path.realpath(path)
else:
return list(map(lambda x: self.realpath(x), path))
def join(self, *args):
""" join paths """
args = args[0] if len(args) == 1 else args
return os.path.join(*args)
def copy(self, path1, path2, makedirs=True):
"""copy one file to another path"""
path1 = os.path.abspath(path1)
path2 = os.path.abspath(path2)
if os.path.exists(path1):
if makedirs:
os.makedirs(os.path.dirname(path2), exist_ok=True)
shutil.copy(path1, path2)
def move(self, path1, path2, makedirs=True):
"""copy one file to another path"""
path1 = os.path.abspath(path1)
path2 = os.path.abspath(path2)
if os.path.exists(path1):
if makedirs:
os.makedirs(os.path.dirname(path2), exist_ok=True)
shutil.move(path1, path2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment