导库
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