用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. Windows安装MongoDB .zip绿色版

    本文链接:https://blog.csdn.net/HTouying/article/details/88428452 MongoDB官网下载链接:https://www.mongodb.com/d ...

  2. Link-Cut Tree(LCT) 教程

    目录 前置知识 介绍 Access FindRoot MakeRoot Split Link Cut 关于Splay中操作的一点说明: 模板 前置知识 请先对树链剖分和Splay有所了解.LCT基于树 ...

  3. CommandLineRunner and ApplicationRunner

    1. Run spring boot as a standalone application (non-web) <?xml version="1.0" encoding=& ...

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

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

  5. xpath元素定位方法

    XPath 使用路径表达式来选取 XML 文档中的节点或者节点集.这些路径表达式和我们在常规的电脑文件系统中看到的表达式非常相似.XPath 含有超过 100 个内建的函数.这些函数用于字符串值.数值 ...

  6. Git:本地项目与远程仓库的git/clone

      版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_40197828/article/details/79283278 初识Git命令行将本地项 ...

  7. go GTK msys2

    1 下载安装msys2 MSYS2 (Minimal SYStem 2) 是一个MSYS的独立改写版本,主要用于 shell 命令行开发环境.同时它也是一个在Cygwin (POSIX 兼容性层) 和 ...

  8. Kotlin 1 新运算符

    新运算符: “..” ,"in ","!in" ,"downto","step"注意: 1. 关系运算符的优先级低于算术 ...

  9. leetcode-easy-listnode-234 Palindrome Linked List

    mycode   89.42% # Definition for singly-linked list. # class ListNode(object): # def __init__(self, ...

  10. csp2019 Emiya家今天的饭题解

    qwq 由于窝太菜了,实在是不会,所以在题解的帮助下过掉了这道题. 写此博客来整理一下思路 正文 传送 简化一下题意:现在有\(n\)行\(m\)列数,选\(k\)个数的合法方案需满足: 1.一行最多 ...