add file includes several methods include remove, copy and etc.

This commit is contained in:
Prodesire 2017-11-28 22:55:51 +08:00
parent c0e274b811
commit 96375a1c1f
1 changed files with 70 additions and 0 deletions

70
pydu/file.py Normal file
View File

@ -0,0 +1,70 @@
import os
import sys
import shutil
# todo tests and docs
def makedirs(path, mode=0o755, *, ignore_errors=False):
try:
os.makedirs(path, mode, exist_ok=True)
except Exception:
if not ignore_errors:
raise OSError('Create dir: {} error'.format(path))
def remove(path, *, ignore_errors=False, onerror=None):
if ignore_errors:
def onerror(*args):
pass
elif onerror is None:
def onerror(*args):
raise OSError('Remove path: {} error'.format(path))
if os.path.isdir(path):
shutil.rmtree(path, ignore_errors=ignore_errors, onerror=onerror)
else:
try:
os.remove(path)
except Exception:
onerror(os.remove, path, sys.exc_info())
def removes(*paths, ignore_errors=False, onerror=None):
for path in paths:
remove(path, ignore_errors=ignore_errors, onerror=onerror)
def open_file(path, mode='wb+', *, buffer_size=-1, ignore_errors=False):
f = None
try:
if path and not os.path.isdir(path):
makedirs(os.path.dirname(path))
f = open(path, mode, buffer_size)
except Exception:
if not ignore_errors:
raise OSError('Open file: {} error'.format(path))
return f
def link(src, dst, *, overwrite=False, ignore_errors=False):
try:
if os.path.exists(dst):
if overwrite:
remove(dst)
else:
return
os.link(src, dst)
except Exception:
if not ignore_errors:
raise OSError('Link {} to {} error'.format(dst, src))
def copy(src, dst, *, ignore_errors=False, follow_symlinks=True):
try:
if os.path.isdir(src):
shutil.copytree(src, dst, symlinks=not follow_symlinks)
else:
shutil.copy(src, dst, follow_symlinks=follow_symlinks)
except Exception:
if not ignore_errors:
raise OSError('Copy {} to {} error'.format(src, dst))