1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- # --coding:utf-8--
- '''
- 文件操作
- '''
- import os
- import shutil
- from shutil import copyfile
- def save_file(file: bytes, filename):
- '''
- bytes保存文件
- :param file:
- :param filename:
- :return:
- '''
- try:
- with open(filename, "wb") as fw:
- fw.write(file)
- fw.close()
- return True
- except Exception:
- return False
- def generate_directory(dir_path: str) -> bool:
- '''
- 生成文件夹
- :param dir_path:文件夹路径
- :return:
- '''
- try:
- if not os.path.exists(dir_path):
- os.makedirs(dir_path)
- os.chmod(dir_path, 0o777)
- except Exception:
- return False
- return True
- def del_directory(dir_path: str):
- '''
- 删除文件夹
- :param dir_path: 文件夹路径
- :return:
- '''
- if os.path.exists(dir_path):
- shutil.rmtree(dir_path)
- def file_copy(source_path: str, target_path: str):
- """
- 文件copy到目标文件夹
- :param source_path: 文件原路径
- :param target_path: 目标文件夹
- :return:
- """
- try:
- copyfile(source_path, target_path)
- except IOError as e:
- return False
- return True
|