import os
import errno
import shutil
def empty_dir(path):
"""
Delete all files and folders in a directory
:param path: string, path to directory
:return: nothing
"""
for the_file in os.listdir(path):
file_path = os.path.join(path, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print ('Warning: {}'.format(e))
def create_dir(path):
"""
Creates a directory
:param path: string
:return: nothing
"""
try:
os.makedirs(path)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
def prepare_dir(path, empty=False):
"""
Creates a directory if it soes not exist
:param path: string, path to desired directory
:param empty: boolean, delete all directory content if it exists
:return: nothing
"""
if not os.path.exists(path):
create_dir(path)
if empty:
empty_dir(path)
utils
最新推荐文章于 2024-05-09 13:14:52 发布