python自动化:文件的处理操作

导库

import os
import shutil
import time

import win32file

常用操作

def copy(objPath, dstPath, isCover):
    """
    功能: 复制文件或文件夹到指定路径。可重命名
    :param objPath: 原文件路径
    :param dstPath: 指定路径
    :param isCover: 是否覆盖
    :return: 运行结果
    """
    try:
        if not os.path.exists(objPath) or not os.path.exists(dstPath):
            raise Exception("非法的路径参数")
        if os.path.isfile(objPath):
            if isCover:
                isCover = 0
            else:
                isCover = 1
            win32file.CopyFile(objPath, dstPath, isCover)
        else:
            if os.path.exists(dstPath):
                if isCover:
                    shutil.rmtree(dstPath)
                    shutil.copytree(objPath, dstPath)
            else:
                shutil.copytree(objPath, dstPath)
        return True
    except Exception as e:
        return e
def moveFile(objPath, dstPath, isCover):
    """
    功能: 移动文件或文件夹。可重命名。如果目标路径下有该名称文件,不覆盖的情况下,原文件不动
    :param objPath: 原文件路径
    :param dstPath: 目标路径
    :param isCover: 是否覆盖
    :return: 运行结果
    """
    try:
        if not os.path.exists(objPath):
            raise Exception("非法的路径参数")
        if os.path.exists(dstPath):
            if isCover:
                if os.path.isdir(dstPath):
                    shutil.rmtree(dstPath)
                else:
                    delFile(dstPath)
                shutil.move(objPath, dstPath)
        else:
            shutil.move(objPath, dstPath)
        return True
    except Exception as e:
        return e
def delFile(path):
    """
    功能: 删除文件或文件夹
    :param path: 要删除的文件/文件夹路径
    :return: 运行结果
    """
    try:
        if os.path.exists(path):
            if os.path.isfile(path):
                os.remove(path)
            else:
                shutil.rmtree(path)
            return True
        raise Exception("非法的路径参数")
    except Exception as e:
        return e
def getFileName(filePath):
    """
    功能: 获取文件或文件夹名字
    :param filePath: 文件或文件夹路径
    :return: 名称
    """
    try:
        if isinstance(filePath, str) and os.path.isfile(filePath):
            return os.path.basename(filePath)
        raise Exception("非法的参数类型")
    except Exception as e:
        return e
def reName(name, dstName):
    """
    重命名文件
    :param name: 原文件名称(包含路径)
    :param dstName: 新名词(包含路径)
    :return: 运行结果
    """
    try:
        if isinstance(name, str) and isinstance(dstName, str) and os.path.exists(name):
            os.rename(name, dstName)
            return True
        raise Exception("非法的参数")
    except Exception as e:
        return e
def readFile(path):
    """
    读取文件内容
    :param path: 路径
    :return: 内容
    """
    try:
        if not os.path.isfile(path):
            raise Exception("非法的参数")
        content = ''
        with open(path, 'r', encoding='utf-8') as f:
            text = f.read()
            content += text
        return content
    except Exception as e:
        return e
def writeFile(path, mode, encoding, text):
    """
    写入文件
    :param path: 文件路径,不存在创建
    :param mode: 打开模式。w:写,如果该文件已存在,则覆盖;a:读写,如果该文件已存在,则在文档最后添加;wb:同w,二进制形式;ab:同a,二进制形式
    :param encoding: 编码格式
    :param text: 要写入的文本
    :return: 运行结果
    """
    try:
        if not os.path.isfile(path):
            raise Exception("非法的参数")
        f = open(path, mode=mode, encoding=encoding)
        f.write(text)
        f.close()
        return True
    except Exception as e:
        return e
def createDir(path):
    """
    创建文件夹
    :param path: 文件夹路径
    :return: 运行结果
    """
    try:
        if os.path.exists(path):
            raise Exception("路径已存在")
        os.mkdir(path)
        return True
    except Exception as e:
        return e
def getFiles(path):
    """
    获取指定目录下的文件或文件夹名称
    :param path: 指定目录
    :return: 文件或文件夹名称,数组类型
    """
    try:
        if os.path.exists(path):
            return os.listdir(path)
        raise Exception("非法的路径参数")
    except Exception as e:
        return e
def formatTime(dTime):
    """
    格式化时间的函数
    :param dTime: 时间戳
    :return: 格式化后的时间
    """
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(dTime))

def getFileInfo(path):
    """
    获取文件信息
    :param path: 文件路径
    :return: 文件信息。字典类型,包括大小,创建时间,最后一次修改时间
    """
    try:
        if os.path.isfile(path):
            fileInfo = {}
            fileObj = os.stat(path)
            fileInfo['byte'] = fileObj.st_size
            fileInfo['createTime'] = formatTime(fileObj.st_ctime)
            fileInfo['lastReviseTime'] = formatTime(fileObj.st_mtime)
            return fileInfo
        raise Exception("非法的路径参数")
    except Exception as e:
        return e
def isExists(path):
    """
    判断文件/文件夹是否存在
    :param path: 文件路径
    :return: 运行结果
    """
    try:
        if os.path.exists(path):
            return True
        return False
    except Exception as e:
        return e
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穿背心的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值