python3的一些文件操作的脚手架
用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的一些文件操作的脚手架的更多相关文章
- Python3中IO文件操作的常见用法
首先创建一个文件操作对象: f = open(file, mode, encoding) file指定文件的路径,可以是绝对路径,也可以是相对路径 文件的常见mode: mode = “r” # ...
- python3.x Day3 文件操作
文件操作:操作文件实际是4步骤1.描述文件是哪个 2.打开文件 3.操作文件 4.关闭文件 1.打开文件使用open方法,代码举例: data=open("wait_you",en ...
- Python3之json文件操作
json函数 使用json函数之前,首先需要导入json模块,import json 1).json.dumps()函数 该函数是将 Python 对象编码成 JSON 字符串,例如: import ...
- python从入门到大神---4、python3文件操作最最最最简单实例
python从入门到大神---4.python3文件操作最最最最简单实例 一.总结 一句话总结: python文件操作真的很简单,直接在代码中调用文件操作的函数比如open().read(),无需引包 ...
- Python基础:Python函数、文件操作、递归
函数参数 函数参数包括位置参数,关键字参数,动态参数(*args, **args)三种. 传参的过程是形式参数的赋值. *args传入的参数是元组形式,**args传入的参数是字典形式. 示例代码如下 ...
- Python3学习之路~2.7 文件操作
对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下 Somehow, it seems the love I knew was always the ...
- Python3 文件操作(十六)
一 文件操作 1.介绍 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众 ...
- Python3.x:open()文件操作
Python3.x:open()文件操作 open/文件操作: #open(路径+文件名,读写模式) #读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式 f=ope ...
- Python3基础(3)集合、文件操作、字符转编码、函数、全局/局部变量、递归、函数式编程、高阶函数
---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...
随机推荐
- mysql内存分配问题
云数据库 MySQL 的内存是重要的性能参数,常出现由异常 SQL 请求以及待优化的数据库导致的内存利用率升高的情况,严重时还会出现由于 OOM 导致实例发生 HA 切换,影响业务的稳定及可用性. M ...
- VS Code 最好用的 Markdown 插件
对经常使用 Markdown 写东西的工程师来说找到称手好用的 Markdown 编辑器非常重要. 目前为止 VS Code 最好用的插件是 Markdown Preview Enhanced . 各 ...
- 我不熟悉的set
同样的我着重介绍那些我不怎么用到的系列,同时,常用的我就点一下. 我们都知道set底层是用红黑树实现的,红黑树是一种已排序的树,所以我们通过迭代器来访问节点元素的时候,并不可以改变它,如果随意改变,那 ...
- java取小数点后两位
package com.yonyou.sud.algorithm; import java.math.BigDecimal;import java.text.DecimalFormat;/*** ja ...
- 解析XML的几种方式:DOM、SAX、PULL
DOM解析 解析器读入整个文档,然后构建一个主流内存的树结构,然后代码就可以使用dom接口来操作这个树结构. 优点: 整个文档树在内存中,便于操作:支持删除.修改.重新排列等多种功能. 通过树形结构存 ...
- 剑指offer:关于复制构造函数
1:首先参看代码: #include "stdafx.h" #include "iostream" using namespace std; class A { ...
- Linux常用命令及操作(第二弹)
Ctrl l清屏 Ctrl d关闭终端 Ctrl Alt T打开终端 pwd 查看当前的目录 Shift Ctrl C复制 Shift Ctrl V粘贴 Shift Ctrl N打开新的终端 F11 ...
- 1. 参数的传入和添加 argparse.ArgumentParser()
# Edit configuration 传入的参数使用的是--file_dir picture, 获取使用的是argv.file_dir import argparse, sys def parse ...
- Gradle 依赖
在开发中,我们经常使用compile,api,implementation引入库,这三种是有区别的. 1 api和compile api和compile关键字作用效果是一样的,使用时可以互相替换. 实 ...
- golang 开源项目: 配置解析模块--config
在golang中,配置文件经常使用json格式.json格式的语法,有些繁琐,尤其是出现嵌套的时候,每一块都需要大括号包裹,看起来很臃肿. 本着简单易用的原则,个人开发了一个配置解析模块config, ...