用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. 洛谷P3294 [SCOI2016]背单词——题解

    题目传送 阅读理解题题意解释可以看这位大佬的博客. 发现求后缀与倒序求前缀是等价的,而找前缀自然就想到了trie树.将所有字符串翻转后再建入trie树中,再对每一个字符串翻转后从trie树中找前缀,就 ...

  2. PTA 道长你想怎么死

    道长你想怎么死 (25 分) 故事:[ 他身着白衣,撑着伞朝我走来.说要送我回家.而我早已陷入他那对深邃的眼眸中,心内一阵悸动.他一把拉我入伞下.我得知他是山上的道士,也刚好下山采药.他把伞赠予我,一 ...

  3. HTML和CSS 入门系列(一):超链接、选择器、颜色、盒模式、DIV布局、图片

    一.超链接 二.CSS选择器 CSS的全称叫做: Cascading Style Sheets 级联样式表的缩写. 2.1 类型选择器 2.2 派生选择器 2.3 伪类选择器 <style &g ...

  4. TCP之LAST_ACK状态

    前提: A:主动关闭: B:被动关闭: A执行主动关闭,发送FIN,B收到FIN,发送ACK,进入CLOSE_WAIT,B发送FIN,进入LAST_ACK等待最后一个ACK到来: 关闭方式: (1) ...

  5. tp5获取器的用法。

    1.命名规则   get + 属性名的驼峰命名+ Attr      ------>在相应的model中创建方法 例如: protected function getSexAttr($value ...

  6. spark 笔记 12: Executor,task最后的归宿

    spark的Executor是执行task的容器.和java的executor概念类似. ===================start executor runs task============ ...

  7. Linux高级调试与优化——Address Sanitizer

    Address Sanitizer ASAN最早可以追溯到 LLVM 的 sanitizers项目(https://github.com/google/sanitizers),这个项目包含了Addre ...

  8. navivate 下载

    https://www.jianshu.com/p/5f693b4c9468?mType=Group

  9. android 面试汇总<一>

    1.1 Android Activity Q:说下Activity的生命周期? 技术点:Activity生命周期 思路:分条解释Activity从创建到销毁整个生命周期中涉及到的方法及作用 参考回答: ...

  10. Slider 滑块

    通过拖动滑块在一个固定区间内进行选择 ¶基础用法 在拖动滑块时,显示当前值 通过设置绑定值自定义滑块的初始值 <template> <div class="block&qu ...