对文件相关处理是脚本中最常见的,下面列举几种实用的案例:

批量删除:

(1)删除指定目录,指定后缀文件

例:删除目录J:/start下的 .log与.tmp结尾文件

def del_files(path, filters):
if os.path.exists(path) and os.path.isdir(path):
for root, dirs, files in os.walk(path):
for name in files: # name.find(".tmp")>0
for subfix in filters:
if name.endswith(subfix):
os.remove(os.path.join(root, name))
print ("Delete File: " + os.path.join(root, name)) def test_del_files():
filters = [".log",".tmp"]
del_files("J:/StartUML",filters)

(2)只保留特定文件

def del_all(dir, retain_file):
if os.path.exists(dir) and os.path.isdir(dir):
dir_content = [x for x in os.listdir(dir) if x != retain_file]
for f in dir_content:
fpath = os.path.join(dir, f)
if os.path.isdir(fpath):
shutil.rmtree(fpath)
else:
os.remove(fpath) del_all("J:/StartUML","11.txt")

批量复制与移动:

(1)复制处理:

#方案一
def copy_all(sourceDir,targetDir):
if os.path.exists(sourceDir):
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir, file)
targetFile = os.path.join(targetDir, file)
if os.path.isfile(sourceFile):
if not os.path.exists(targetDir):
os.mkdir(targetDir)
# 如果目的路径里面不存在某个文件或者存在那个同名文件但是文件有残缺,则复制,否则跳过
if not os.path.exists(targetFile) or \
(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
shutil.copy(sourceFile,targetFile)
if os.path.isdir(sourceFile):
copy_all(sourceFile, targetFile) #方案二
def copy_files(src_dir,dest_dir,isonelevel):
if os.path.exists(src_dir) and os.path.isdir(src_dir):
if not os.path.exists(dest_dir):
os.mkdir(dest_dir)
for parent, dirnames, filenames in os.walk(src_dir):
# if parent.startswith(src_dir):
if isonelevel:
dstdir = dest_dir
else:
dstdir = parent.replace(src_dir,dest_dir,1)
for dirname in dirnames:
os.mkdir(os.path.join(dstdir,dirname))
for fname in filenames:
shutil.copy(os.path.join(parent, fname), os.path.join(dstdir, fname)) # copy_all("J:/ZIMU","J:/StartUML")

(2)移动处理

#walk遍历处理实现
def move_files(src_dir, dst_dir):
if os.path.exists(src_dir) and os.path.isdir(src_dir):
if not os.path.exists(dst_dir):
os.mkdir(dst_dir)
for pdir ,dirs, fnames in os.walk(src_dir,topdown=True):
newdstdir = pdir.replace(src_dir,dst_dir,1)
if not os.path.exists(newdstdir):
os.mkdir(newdstdir)
for fn in fnames:
os.rename(os.path.join(pdir,fn),os.path.join(newdstdir,fn))
for dir in dirs:
dstSource = os.path.join(newdstdir,dir)
if not os.path.exists(dstSource):
os.mkdir(dstSource)
shutil.rmtree(src_dir) #递归实现
def move_recursive(sourceDir,targetDir):
if os.path.exists(sourceDir):
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir, file)
targetFile = os.path.join(targetDir, file)
if not os.path.exists(targetDir):
os.mkdir(targetDir)
if os.path.isfile(sourceFile):
os.rename(sourceFile,targetFile)
elif os.path.isdir(sourceFile):
move_recursive(sourceFile,targetFile) def move_all(sourceDir,targetDir):
move_recursive()
shutil.rmtree(sourceDir) # move_all("J:/StartUML/AGirls","J:/StartUML/ABoys")

搜索与查找:

(1)查找指定文件名称文件

# 指定目录及其子目录中查找文件名含有关键字的文件
def search_file_pattern_name1(path, word):
for filename in os.listdir(path):
fp = os.path.join(path, filename)
if os.path.isfile(fp) and word in filename:
print(fp)
elif os.path.isdir(fp):
search_file_pattern_name1(fp, word) # search_file("J:/AndroidSrc4.2/packages" ".png")
def search_file_pattern_name2(dirname,keyworld):
results = []
for root, dirs, files in os.walk(dirname):
results += [os.path.relpath(os.path.join(root, x), start = dirname) for x in files if keyworld in x]
for result in results:
print(result)

(2)查找文本内容包含指定关键词的所以文件,输出该文件路径

def search_file_txtcontent1(dir, word,isaccurate):
if os.path.exists(dir):
for filename in os.listdir(dir):
fp = os.path.join(dir, filename)
if os.path.isfile(fp):
with open(fp) as f:
num = 0
for line in f:
num += 1
if word in line:
if isaccurate:
dSearch = line.split()
for search_word in dSearch:
if search_word == word:
print ("accurate find word ", "fileneme=",filename, " line =" , num)
else:
print ("blur find word ", "fileneme=", filename, " line =", num)
# break
elif os.path.isdir(fp):
search_file_txtcontent1(fp, word,isaccurate) # search_file_txtcontent1("J:/AndroidSrc4.2/packages/apps/Launcher2" ,"onCreate",False) # fileinput模块可以遍历文本文件的所有行.它的工作方式和readlines很类似,不同点在于,
# 它不是将全部的行读到列表中而是创建了一个xreadlines对象.
def search_file_txtcontent2(dir_path,searchKey,isaccurate):
# pattern = "\d{3}-\d{3}-\d{4}" # 如800-333-1212
if os.path.exists(dir_path):
for pdir, subdirs, subfiles in os.walk(dir_path):
for fname in subfiles:
fn = os.path.join(pdir,fname)
if os.path.splitext(fn)[1] == ".java":
finput = fileinput.input(fn)
for eachline in finput:
if isaccurate:
for m in re.finditer(r"\bonCreate\b", eachline):
if m.group(0):
print("accurate find ============")
print ('filename:', fileinput.filename(), 'line:', fileinput.lineno(), eachline)
else:
a = re.search(searchKey, eachline)
if a:
print("============")
print ('filename:', fileinput.filename(), 'line:', fileinput.lineno(), eachline) # search_file_txtcontent2("J:/AndroidSrc4.2/packages/apps/Launcher2","onCreate",True)

(3)文本替换处理,将文本内指定原内容替换为新的内容

#方案一,单文件处理
def search_replace_content(src_file, oldWorld, newWorld):
if os.path.exists(src_file):
# print("tempfile name is", "=>", file) # TemporaryFile创建的临时文件的名字
if os.path.exists(src_file):
fopen = open(src_file, 'r')
else:
print("file %s not found" % src_file)
sys.exit() temp_file = tempfile.mktemp()
file_dst = open(temp_file, 'w+b') # 打开临时文件
for line in fopen:
line = re.sub(oldWorld, newWorld, line)
file_dst.write(line) # 把替换后的内容写入到临时文件中
fopen.close()
file_dst.seek(0)
file_dst.close() if os.path.exists(src_file):
os.remove(src_file)
shutil.copy(temp_file, src_file) # copy临时文件到原文件
try:
os.remove(temp_file) # 删掉临时文件
except OSError:
pass #方案二,多文件处理
def search_replace_bigtxt(dir_search,oldKey, newKey):
for parent_dir, subdirs, files in os.walk(dir_search):
for file in files:
fname = os.path.join(dir, file)
inFile = codecs.open(fname, "r", "utf-8")
outFile = codecs.open(fname + ".new", "w", "utf-8")
for line in inFile:
newline = line.replace(oldKey, newKey)
outFile.write(newline)
inFile.close()
outFile.close()
os.rename(fname + ".new", fname)

多组词替换处理

def easy2_replace_txt():
replacements = {'zero': '0', 'temp': 'bob', 'garbage': 'nothing'}
with open('path/to/input/file') as infile, open('path/to/output/file', 'w') as outfile:
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
outfile.write(line)

Python3 文件处理相关脚本的更多相关文章

  1. 将 Python3 文件打包成 exe 文件

    我们用 Python 写好的代码,如何给别人在没有配置 Python 环境的情况下直接使用呢?尤其是面向 windows 众. 因为 Python 是一门解释性的语言,离开了 Python 解释器,P ...

  2. linux用户、文件权限相关命令

    root 现代操作系统一般属于多用户的操作系统,也就是说,同一台机器可以为多个用户建立账户,一般这些用户都是为普通用户,这些普通用户能同时登录这台计算机,计算机对这些用户分配一定的资源. 普通用户在所 ...

  3. shell基础、变量、相关脚本

    目录 一.shell基础 书写规范 引号 配置文件 read交互 脚本调式 小节总结 二.变量 变量类型 位置变量 状态变量 替换和删除 变量补充 变量运算 小节总结 三.相关脚本面试题 统计hist ...

  4. 【转载】]基于RedHatEnterpriseLinux V7(RHEL7)下SPEC CPU 2006环境搭建以及测试流程 介绍、安装准备、安装、config文件以及运行脚本介绍

    https://www.codetd.com/article/1137423 <版权声明:本文为博主原创文章,未经博主允许不得转载> 本次利用SPECCPU2006测试工具来进行Intel ...

  5. 【转载】基于RedHatEnterpriseLinux V7(RHEL7)下SPEC CPU 2006环境搭建以及测试流程(之一)——介绍、安装准备、安装、config文件以及运行脚本介绍

    基于RedHatEnterpriseLinux V7(RHEL7)下SPEC CPU 2006环境搭建以及测试流程(之一)--介绍.安装准备.安装.config文件以及运行脚本介绍 其他 2018-0 ...

  6. Shell文件权限和脚本执行

    一.预备知识 1.shell的作用   2.常识 (1)Tab键自动补全   使用Terminal时,输入命令的前几个字母,敲tab会自动补全命令或文件名.目录等. 好处:操作速度更快:不容易出错: ...

  7. 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本

    摘自:http://blog.csdn.net/forandever/article/details/5711319 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本 ...

  8. unity3d 特殊文件夹和脚本编译顺序

    unity3d 特殊文件夹和脚本编译顺序 转自http://blog.csdn.net/u010019717/article/details/40474631 大多数情况下,您可以选择任何你喜欢的文件 ...

  9. 初步学习nodejs,业余用node写个一个自动创建目录和文件的小脚本,希望对需要的人有所帮助

    初步学习nodejs,业余用node写个一个自动创建目录和文件的小脚本,希望对需要的人有所帮助,如果有bug或者更好的优化方案,也请批评与指正,谢谢,代码如下: var fs = require('f ...

  10. 自动删除超过30天文件的vbs脚本【转发】

    利用代码制作自动删除超过30天的文件及文件夹的vbs脚本,定期清理文件夹中长时间无用文件. 1.首先在新建一个文本文档,粘贴代码(代码可通过添加微信公众号vbs_edit(VBS脚本之家)回复018获 ...

随机推荐

  1. MDK GCC调试

    openocd调试 https://blog.csdn.net/chunyexixiaoyu/article/details/120448515

  2. c++ class基础知识

    目录 class 创建对象 通过对象指针访问类成员 类方法可以定义在类体外部(要用到域解析符:. 类成员/类方法访问权限 构造函数 析构函数Destructor this指针 静态变量 静态函数 co ...

  3. Android中添加set文件

    一般情况下,set文件在anim目录下面,如果没有anim目录,就新建一个. 1. 右键点击anim,选择new->Animation Resource File 2. 选择类型为set,点击O ...

  4. Nginx--logrotate日志切割打包

    1.系统是默认安装的,查看系统是否安装logrotate centos rpm -ql logrotate /etc/cron.daily/logrotate /etc/logrotate.conf ...

  5. SVG 从入门到后悔,怎么不早点学起来(图解版)

    点赞 + 关注 + 收藏 = 学会了 作为一只前端,只懂 Vue.React 感觉已经和大家拉不开距离了. 可视化.机器学习等领域 JS 都有涉及到,而可视化方面已经被很多领域用到,比如大屏项目. 可 ...

  6. nuxt 服务端渲染注意事项

    1.路由 nuxt按照 pages 文件夹的目录结构自动生成路由 http://localhost:3000/user/reg 相当于 去访问 pages文件夹 下的 user文件夹 下的 reg.v ...

  7. winform 中 label透明化

    label1.BackColor = Color.Transparent;//设置背景颜色为透明 label1.Parent = pictureBox1;//将pictureBox1设为标签的父控件, ...

  8. 华为&思科设备默认的路由协议优先级

    华为&思科设备默认的路由协议优先级 华为设备默认路由协议优先级 在华为的设备中,路由器分别定义了外部优先级和内部优先级. 外部优先级是指用户可以手工为各路由协议配置的优先级; 内部优先级不能被 ...

  9. leetcode91解码

    解码,dp,注意特殊情况 def numDecodings( s: str) -> int: if len(s) < 1: return 1 if s[0] =='0': return 0 ...

  10. react native 第三方富文本编辑器 wxik/react-native-rich-editor(在移动端使用)

    //更新2021年8月23日 (1)wxik/react-native-rich-editor  个人认为功能比较全,推荐使用 关于使用的案例,官网上有,我直接粘贴我遇到的几个问题 1. 软键盘弹出时 ...