You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
231 lines
4.8 KiB
231 lines
4.8 KiB
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
# @Time : 2022/9/10 10:45
|
|
# @Author : old tom
|
|
# @File : fu_file.py
|
|
# @Project : Futool
|
|
# @Desc : 文件操作
|
|
import shutil
|
|
import os
|
|
import hashlib
|
|
import zipfile
|
|
from pathlib import Path
|
|
from futool.core.fh_file_path import loop_mk_dir, parent_path, exist
|
|
from enum import Enum
|
|
|
|
|
|
class FileSuffix(Enum):
|
|
"""
|
|
常见文件后缀
|
|
"""
|
|
XLSX = '.xlsx'
|
|
XLS = '.xls'
|
|
DOC = '.doc'
|
|
DOCX = '.docx'
|
|
PPT = '.ppt'
|
|
PPTX = '.pptx'
|
|
EXE = '.exe'
|
|
MSI = '.msi'
|
|
ISO = '.iso'
|
|
PGP = '.pgp'
|
|
PNG = '.png'
|
|
JPG = 'jpg'
|
|
JPEG = '.jpeg'
|
|
GIF = '.gif'
|
|
WAV = '.wav'
|
|
JAR = '.jar'
|
|
PY = '.py'
|
|
BAT = '.bat'
|
|
DLL = '.dll'
|
|
ZIP = '.zip'
|
|
SEVEN_ZIP = '.7z'
|
|
TAR = '.tar'
|
|
RAR = '.rar'
|
|
|
|
|
|
class FileNotExistError(Exception):
|
|
def __init__(self, msg=''):
|
|
Exception.__init__(self, msg)
|
|
|
|
|
|
def split_text():
|
|
pass
|
|
|
|
|
|
def split_json():
|
|
pass
|
|
|
|
|
|
def compress(path, z_path, z_name='zip'):
|
|
"""
|
|
压缩文件或文件夹
|
|
:param path:
|
|
:param z_path: 压缩后路径
|
|
:param z_name: 压缩格式 zip rar gz
|
|
:return:
|
|
"""
|
|
pass
|
|
|
|
|
|
def un_compress(path):
|
|
"""
|
|
解压
|
|
:param path:
|
|
:return:
|
|
"""
|
|
pass
|
|
|
|
|
|
def md5(file_path) -> str:
|
|
"""
|
|
文件MD5
|
|
:param file_path:
|
|
:return:
|
|
"""
|
|
if not exist(file_path):
|
|
raise FileNotExistError('文件不存在')
|
|
with open(file_path, 'rb') as f:
|
|
data = f.read()
|
|
return hashlib.md5(data).hexdigest()
|
|
|
|
|
|
def move(src, dst):
|
|
"""
|
|
移动文件夹或文件
|
|
:param src: 源
|
|
:param dst: 目标
|
|
:return:
|
|
"""
|
|
shutil.move(src, dst)
|
|
|
|
|
|
def copy(src, dst, override=False):
|
|
"""
|
|
复制
|
|
:param src:
|
|
:param dst:
|
|
:param override:
|
|
:return:
|
|
"""
|
|
if override and exist(dst):
|
|
os.remove(dst)
|
|
shutil.copy(src, dst)
|
|
|
|
|
|
def copy_dir(src, dst, override=False) -> str:
|
|
"""
|
|
复制文件夹
|
|
:param src: 源目录
|
|
:param dst: 目的目录
|
|
:param override: 是否覆盖
|
|
:return:
|
|
"""
|
|
if override and exist(dst):
|
|
os.remove(dst)
|
|
return shutil.copytree(src, dst, dirs_exist_ok=override)
|
|
|
|
|
|
def copy_file(src, dst, override=False) -> str:
|
|
"""
|
|
复制文件
|
|
:param src: 源文件
|
|
:param dst: 目的文件或目的目录
|
|
:param override: 是否覆盖
|
|
:return:
|
|
"""
|
|
if override and exist(dst):
|
|
os.remove(dst)
|
|
if Path(dst).is_dir():
|
|
dst = os.path.join(dst, file_full_name(src))
|
|
return shutil.copyfile(src, dst)
|
|
|
|
|
|
def rename(file_path, neo_name) -> Path:
|
|
"""
|
|
重命名
|
|
:param file_path: 文件路径
|
|
:param neo_name: 新命名
|
|
:return:
|
|
"""
|
|
return Path(file_path).rename(parent_path(file_path).joinpath(neo_name))
|
|
|
|
|
|
def delete(file_path):
|
|
"""
|
|
删除文件
|
|
:param file_path:
|
|
:return:
|
|
"""
|
|
if exist(file_path):
|
|
os.remove(file_path)
|
|
|
|
|
|
def touch(file_path, mode=0o777, cover=True):
|
|
"""
|
|
创建文件
|
|
:param file_path: 文件路径
|
|
:param mode: 权限
|
|
:param cover: 是否覆盖
|
|
:return:
|
|
"""
|
|
parent = parent_path(file_path)
|
|
if not parent.exists():
|
|
loop_mk_dir(str(parent), mode=mode, exist_ok=cover)
|
|
Path(file_path).touch(mode=mode, exist_ok=cover)
|
|
|
|
|
|
def file_name(file_path) -> str:
|
|
"""
|
|
获取文件名
|
|
:param file_path:
|
|
:return:
|
|
"""
|
|
full_name = file_full_name(file_path)
|
|
return full_name.split(sep='.')[0] if (full_name and '.' in full_name) else full_name
|
|
|
|
|
|
def file_full_name(file_path) -> str:
|
|
"""
|
|
文件全名,带后缀
|
|
:param file_path:
|
|
:return:
|
|
"""
|
|
return Path(file_path).name
|
|
|
|
|
|
def suffix(file_path) -> str:
|
|
"""
|
|
文件后缀
|
|
:return:
|
|
"""
|
|
return Path(file_path).suffix
|
|
|
|
|
|
def suffixes(file_path) -> list:
|
|
"""
|
|
多后缀,例:xxx.tar.gz
|
|
:param file_path:
|
|
:return: [.tar,.gz]
|
|
"""
|
|
return Path(file_path).suffixes
|
|
|
|
|
|
def file_info(file_path):
|
|
"""
|
|
返回文件信息
|
|
:param file_path:
|
|
:return:
|
|
st_mode=33206 文件模式:包括文件类型和文件模式位(即权限位)
|
|
st_ino=281474976714543 与平台有关,但如果不为零,则根据 st_dev 值唯一地标识文件。
|
|
通常: 在 Unix 上该值表示索引节点号 (inode number)。 在 Windows 上该值表示 文件索引号 。
|
|
st_dev=10943705 该文件所在设备的标识符。
|
|
st_nlink=1 硬链接的数量。
|
|
st_uid=0 文件所有者的用户 ID。
|
|
st_gid=0 文件所有者的用户组 ID。
|
|
st_size=453 文件大小(以字节为单位)
|
|
st_atime=1662966762 最近的访问时间,以秒为单位
|
|
st_mtime=1652331424 最近的修改时间,以秒为单位
|
|
st_ctime=1652331424 在 Windows 上表示创建时间,以秒为单位 在 Unix 上表示最近的元数据更改时间
|
|
"""
|
|
return Path(file_path).stat()
|