热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python3文件处理相关脚本

对文件相关处理是脚本中最常见的,下面列举几种实用的案例:批量删除:(1)删除指定目录,指定后缀文件例:删除目录J:start下的.log与.tmp结尾文件defdel_files(path,

 

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

 

批量删除:

(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)

 


推荐阅读
  • 如何使用Python从工程图图像中提取底部的方法?
    本文介绍了使用Python从工程图图像中提取底部的方法。首先将输入图片转换为灰度图像,并进行高斯模糊和阈值处理。然后通过填充潜在的轮廓以及使用轮廓逼近和矩形核进行过滤,去除非矩形轮廓。最后通过查找轮廓并使用轮廓近似、宽高比和轮廓区域进行过滤,隔离所需的底部轮廓,并使用Numpy切片提取底部模板部分。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 本文介绍了Perl的测试框架Test::Base,它是一个数据驱动的测试框架,可以自动进行单元测试,省去手工编写测试程序的麻烦。与Test::More完全兼容,使用方法简单。以plural函数为例,展示了Test::Base的使用方法。 ... [详细]
  • 本文介绍了在使用Python中的aiohttp模块模拟服务器时出现的连接失败问题,并提供了相应的解决方法。文章中详细说明了出错的代码以及相关的软件版本和环境信息,同时也提到了相关的警告信息和函数的替代方案。通过阅读本文,读者可以了解到如何解决Python连接服务器失败的问题,并对aiohttp模块有更深入的了解。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • 超级简单加解密工具的方案和功能
    本文介绍了一个超级简单的加解密工具的方案和功能。该工具可以读取文件头,并根据特定长度进行加密,加密后将加密部分写入源文件。同时,该工具也支持解密操作。加密和解密过程是可逆的。本文还提到了一些相关的功能和使用方法,并给出了Python代码示例。 ... [详细]
  • java drools5_Java Drools5.1 规则流基础【示例】(中)
    五、规则文件及规则流EduInfoRule.drl:packagemyrules;importsample.Employ;ruleBachelorruleflow-group ... [详细]
  • Python中sys模块的功能及用法详解
    本文详细介绍了Python中sys模块的功能及用法,包括对解释器参数和功能的访问、命令行参数列表、字节顺序指示符、编译模块名称等。同时还介绍了sys模块中的新功能和call_tracing函数的用法。推荐学习《Python教程》以深入了解。 ... [详细]
  • 本文介绍了一个Python函数same_set,用于判断两个相等长度的数组是否包含相同的元素。函数会忽略元素的顺序和重复次数,如果两个数组包含相同的元素,则返回1,否则返回0。文章还提供了函数的具体实现代码和样例输入输出。 ... [详细]
author-avatar
wy
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有