Skip to content

Instantly share code, notes, and snippets.

@AlekseiCherkes
Last active January 31, 2018 20:33
Show Gist options
  • Save AlekseiCherkes/5708168 to your computer and use it in GitHub Desktop.
Save AlekseiCherkes/5708168 to your computer and use it in GitHub Desktop.
Python file management tools.
#############################################################################################
# Usefull file and directory managment functions
#############################################################################################
#
# os.sep -- path separator
#
# -------------------------------------------------------------------------------------------
#
# os.path.isabs(path)
# os.path.isfile(path)
# os.path.isdir(path)
# os.path.islink(path)
#
# os.path.join(path1[, path2[, ...]])
# os.path.normcase(path)
# os.path.normpath(path)
# os.path.relpath(path, start=None)
#
# os.path.split(path) => 'foo/bar/etc' = ('foo/bar', 'etc')
# os.path.splitext(path) => 'foo/bar.ext' = ('foo/bar', '.ext')
#
# -------------------------------------------------------------------------------------------
#
# os.rmdir(path, *, dir_fd=None)
# => remove empty dir
#
# os.mkdir(path, mode=0o777, *, dir_fd=None)
# => Create a directory named path with numeric mode mode
#
# os.makedirs(path, mode=0o777, exist_ok=False)
# => Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory
#
# os.walk(top, topdown=True, onerror=None, followlinks=False)
# => Yields a 3-tuple (dirpath, dirnames, filenames)
#
# -------------------------------------------------------------------------------------------
#
# shutil.copy(src, dst, *, follow_symlinks=True)
# => Copies the file src to the file or directory dst
#
# shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)
# => Recursively copy an entire directory tree rooted at src, returning the destination directory.
# shutil.ignore_patterns(*patterns) can be used to create a callable that ignores names based on glob-style patterns.
#
# shutil.rmtree(path, ignore_errors=False, onerror=None)
# => Delete an entire directory tree
#
# shutil.move(src, dst)
# => Recursively move a file or directory (src) to another location (dst) and return the destination.
#
# -------------------------------------------------------------------------------------------
#
# glob.iglob(pathname)
# => Return a possibly-empty list of path names that match pathname
# 'globstar' option is not supported (**)
#
# -------------------------------------------------------------------------------------------
def del_glob(path_glob):
"""Delete all files and folders given by glob expression"""
for fname in glob.iglob(path_glob):
os.remove(fname)
def clean_dir(dir):
"""Clean all directory context"""
if os.path.exists(dir):
if not os.path.isdir(dir):
raise Exception(dir + ' is not a directory')
shutil.rmtree(dir)
os.mkdir(dir)
def copy_glob_to_folder(src_glob, dst_folder):
"""Copy all files and folders given by glob expression into dst dir"""
if not os.path.isdir(dst_folder):
raise Exception(dst_folder + ' is not a directory')
for n in glob.iglob(src_glob):
if os.path.isfile(n):
shutil.copy(n, dst_folder)
elif os.path.isdir(n):
short_name = os.path.split(n)[-1]
dst_name = os.path.join(dst_folder, short_name)
shutil.copytree(n, dst_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment