用python把原来的脚本重构了一下,其中写了文件操作的一些函数,如下:

import os
import shutil
import hashlib
import stat #查找文件夹中的某个文件
def findMyFileDir(dirPath, findFile):
files = []
dirs = []
for root, dirs, files in os.walk(dirPath, topdown=False):
for file in files:
if file == findFile:
return root
for dir in dirs:
findMyFileDir(os.path.join(root, dir), findFile) #创建一个文件夹
def createDir(dirPath):
os.makedirs(dirPath, exist_ok=True) #删除一个文件
def delFile(filePath):
if os.path.exists(filePath):
os.remove(filePath) #删除文件夹里所有的文件
def delDir(dir):
if(os.path.isdir(dir)):
for f in os.listdir(dir):
delDir(os.path.join(dir, f))
if(os.path.exists(dir)):
os.rmdir(dir)
else:
if(os.path.exists(dir)):
os.remove(dir) #拷贝文件
def copyFile(sourceFilePath, destFilePath):
if not(os.path.exists(sourceFilePath)):
return False if os.path.exists(destFilePath):
if getFileMd5(sourceFilePath) == getFileMd5(destFilePath):
return True
else:
os.remove(destFilePath) destFileDir = os.path.dirname(destFilePath)
os.makedirs(destFileDir, exist_ok=True)
if not(shutil.copyfile(sourceFilePath, destFilePath, follow_symlinks=False)):
return False
return True #拷贝文件夹里的文件
def copyDir(sourceDir, destDir):
if not(os.path.exists(sourceDir)):
return False if os.path.exists(destDir):
shutil.rmtree(destDir) if not(shutil.copytree(sourceDir, destDir, symlinks=True)):
return False
return True #获取文件的md5
def getFileMd5(filePath):
with open(filePath, 'rb') as f:
content = f.read()
hash = hashlib.md5()
hash.update(content)
return hash.hexdigest() #获取一个文件夹里的所有的文件和该文件对应的md5
def dirList(dirPath):
listDict = {}
files = []
dirs = []
for root, dirs, files in os.walk(dirPath, topdown=False, followlinks=True):
for file in files:
filePath = os.path.join(root, file)
listDict[os.path.relpath(filePath, dirPath).replace(
'\\', '/')] = getFileMd5(filePath)
for dir in dirs:
dirList(os.path.join(root, dir))
return listDict #逐行读一个文件,并过来文件中某些行里回车和空格
def readLineForFile(filePath):
f = open(filePath, 'r')
lines = f.readlines()
f.close()
newLines = []
for line in lines:
line = line.replace('\n', '').strip()
if line:
newLines.append(line)
return newLines

python3的一些文件操作的脚手架的更多相关文章

  1. Python3中IO文件操作的常见用法

    首先创建一个文件操作对象: f = open(file, mode, encoding) file指定文件的路径,可以是绝对路径,也可以是相对路径 文件的常见mode: mode = “r”   # ...

  2. python3.x Day3 文件操作

    文件操作:操作文件实际是4步骤1.描述文件是哪个 2.打开文件 3.操作文件 4.关闭文件 1.打开文件使用open方法,代码举例: data=open("wait_you",en ...

  3. Python3之json文件操作

    json函数 使用json函数之前,首先需要导入json模块,import json 1).json.dumps()函数 该函数是将 Python 对象编码成 JSON 字符串,例如: import ...

  4. python从入门到大神---4、python3文件操作最最最最简单实例

    python从入门到大神---4.python3文件操作最最最最简单实例 一.总结 一句话总结: python文件操作真的很简单,直接在代码中调用文件操作的函数比如open().read(),无需引包 ...

  5. Python基础:Python函数、文件操作、递归

    函数参数 函数参数包括位置参数,关键字参数,动态参数(*args, **args)三种. 传参的过程是形式参数的赋值. *args传入的参数是元组形式,**args传入的参数是字典形式. 示例代码如下 ...

  6. Python3学习之路~2.7 文件操作

    对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下 Somehow, it seems the love I knew was always the ...

  7. Python3 文件操作(十六)

    一 文件操作 1.介绍 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众 ...

  8. Python3.x:open()文件操作

    Python3.x:open()文件操作 open/文件操作: #open(路径+文件名,读写模式) #读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式 f=ope ...

  9. Python3基础(3)集合、文件操作、字符转编码、函数、全局/局部变量、递归、函数式编程、高阶函数

    ---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...

随机推荐

  1. netty实现客户端服务端心跳重连

    前言: 公司的加密机调度系统一直使用的是http请求调度的方式去调度,但是会出现网络故障导致某个客户端或者服务端断线的情况,导致很多请求信息以及回执信息丢失的情况,接着我们抛弃了http的方式,改为T ...

  2. HDU 2829 [Lawrence] DP斜率优化

    解题思路 首先肯定是考虑如何快速求出一段铁路的价值. \[ \sum_{i=1}^k \sum_{j=1, j\neq i}^kA[i]A[j]=(\sum_{i=1}^kA[i])^2-\sum_{ ...

  3. Jmeter -- 添加断言,及断言结果

    步骤: 1. 添加响应断言(添加-断言-响应断言) Add -->  Assertions --> Response Assertion 2. 配置断言 判断响应内容中,是否包含关键字“禅 ...

  4. conda install -c anaconda

    有些包在conda默认的channels中不包含,比如cudatoolkit-8.0,cudnn等,这时只需要在conda install指令后加上-c anaconda即可.比如要下载cudatoo ...

  5. AJAX请求和普通HTTP请求区别

    两者本质区别: AJAX通xmlHttpRequest象请求服务器服务器接受请求返数据实现刷新交互 普通http请求通httpRequest象请求服务器接受请求返数据需要页面刷新 AJAX请求 普通请 ...

  6. ubuntu16.04修改host上外網

    1.打开hosts文件: sudo emacs /etc/hosts 2.加入下面的内容 #chrome同步服务器 203.208.46.132 chrome.google.com203.208.46 ...

  7. hdjs---1、hdjs爬坑杂记

    hdjs---1.hdjs爬坑杂记 一.总结 一句话总结: 对hdjs这种文档和完善都不是很好的插件,应该先在项目的空页面试,成功后再用到用了框架的项目中 1.hdjs4.0.18引入select2? ...

  8. laravel 发送html邮件是a标签中的url不显示问题

  9. asp.net 获取服务器及客户端的相关信息

    1. 在ASP.NET中专用属性:获取服务器电脑名:Page.Server.ManchineName获取用户信息:Page.User获取客户端电脑名:Page.Request.UserHostName ...

  10. cinder-----常用命令

    云硬盘的创建查询 #创建卷类型 cinder type-create rbd #rbd是云硬盘类型名称,可自行定义 #查询卷类型 cinder type-list #卷类型扩展规格 cinder ty ...