用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. [CF1204E]Natasha,Sasha and the Prefix Sums 题解

    前言 本文中的排列指由n个1, m个-1构成的序列中的一种. 题目这么长不吐槽了,但是这确实是一道好题. 题解 DP题话不多说,直接状态/变量/转移. 状态 我们定义f表示"最大prefix ...

  2. maven web项目中运行stucts2报404的解决方案

    从这篇文章看见的https://www.cnblogs.com/xxqxxq/p/5938821.html 1.将stucts.xml中所有<action>全部注释掉,重新运行 如果运行成 ...

  3. OkHttp3 使用详解

    一,简介 OkHttp 是一个高效的 HTTP 客户端,具有非常多的优势: 能够高效的执行 http,数据加载速度更快,更省流量 支持 GZIP 压缩,提升速度,节省流量 缓存响应数据,避免了重复的网 ...

  4. js手写笔记

    1.document.write(); 2.document.getElementById("").style.color="red";//sytle.font ...

  5. 【Spark机器学习速成宝典】模型篇08支持向量机【SVM】(Python版)

    目录 什么是支持向量机(SVM) 线性可分数据集的分类 线性可分数据集的分类(对偶形式) 线性近似可分数据集的分类 线性近似可分数据集的分类(对偶形式) 非线性数据集的分类 SMO算法 合页损失函数 ...

  6. du和df目录大小不一致

    最近遇到个问题 df查看基本没有空间了 但是查找大文件 也基本没有 ,原来是之前的大文件删除了 没有生效导致的 用如下命令,查找到相关进程ID 然后kill就行了 lsof | grep delete ...

  7. linux的awk使用

    awk统计password文件中,登陆shell为“/sbin/nologin”的用户个数 [root@localhost ~]# cat passwd | grep "/sbin/nolo ...

  8. How to solve the error "Field service in com.xx.xxx.xxxx required a bean of type 'com.aa.bb.cc' that could not be found."

    When runung a SpringBoot demo, I  got a error as following: *************************** APPLICATION ...

  9. IDEA创建各种不同的工程的方法

    javaWeb工程 maven创建javaSE项目 上面点击next: 项目右下角选择自动导入: maven创建javaWeb工程 项目右下角选择自动导入maven项目 上面创建成功之后发现没有jav ...

  10. foundation DB问题

    1. go get github.com/apple/foundationdb/bindings/go/src/fdb # github.com/apple/foundationdb/bindings ...