本文实例讲述了Python比较文件夹比另一同名文件夹多出的文件并复制出来的方法。分享给大家供大家参考。具体如下:
这个东东本来是做来给公司数据同步用的:新服务器还没正式启用,旧的服务器还在使用,每天都有大量图片传到旧服务器上面,为了避免备份全部图片,所以写了这么个工具。
1. 运行效果如下图所示:
2. Python代码如下:
代码如下:
# coding=gbk
'''
Created on 2011-1-7
@author: HH
'''
import os,ConfigParser
'''
递归列出某目录下的文件,放入List中
'''
def listDir (fileList,path):
files=os.listdir(path)
for i in files:
file_path=path+"\\"+i
if os.path.isfile(file_path):
fileList.append(file_path)
for i in files:
file_path=path+"\\"+i
if os.path.isdir(file_path):
#fileList.append(file_path)
listDir(fileList,file_path)
return fileList
'''
将List中内容写入文件
'''
def writeListToFile(list,path):
strs="\n".join(list)
f=open(path,'wb')
f.write(strs)
f.close()
'''
读入文件内容并放入List中
'''
def readFileToList(path):
lists=[]
f=open(path,'rb')
lines=f.readlines()
for line in lines:
lists.append(line.strip())
f.close()
return lists
'''
比较文件--以Set方式
'''
def compList(list1,list2):
return list(set(list1)-set(list2))
'''
复制List中文件到指定位置
'''
def copyFiles(fileList,targetDir):
for file in fileList:
targetPath=os.path.join(targetDir,os.path.dirname(file))
targetFile=os.path.join(targetDir,file)
if not os.path.exists(targetPath):
os.makedirs(targetPath)
if not os.path.exists(targetFile) or (os.path.exists(targetFile) and os.path.getsize(targetFile)!=os.path.getsize(file)):
print "正在复制文件:"+file
open(targetFile,'wb').write(open(file,'rb').read())
else:
print "文件已存在,不复制!"
if __name__ == '__main__':
path=".svn"
#获取源目录
txtFile="1.txt"
#目录结构输出的目的文件
tdir="cpfile"
#复制到的目标目录
cfFile="config.ini";
#配置文件文件名
fileList=[]
#读取配置文件
if(os.path.exists(cfFile)):
cf=ConfigParser.ConfigParser()
cf.read(cfFile)
path=cf.get("main", "sourceDir")
txtFile=cf.get("main","txtFile")
tdir=cf.get("main","targetDir")
else:
print "配置文件不存在!"
raw_input("\n按 回车键 退出\n")
exit()
if(os.path.exists(txtFile)):
#如果导出的文件存在,就读取后比较
list1=readFileToList(txtFile)
print "正在读取文件列表……"
fileList=listDir (fileList,path)
print "正在比较文件……"
list_res=compList(fileList,list1)
if len(list_res)>0:
print "以下是原目录中不存在的文件:\n"
print "\n".join(list_res)
print "\n共计文件数:"+str(len(list_res))+"\n"
if raw_input("\n是否复制文件?(y/n)")!='n':
copyFiles(list_res,tdir)
else:
print "没有不相同的文件!"
else:
#如果导出的文件不存在,则导出文件
print "正在读取文件列表……"
fileList=listDir (fileList,path)
writeListToFile(fileList,txtFile)
print "已保存到文件:"+txtFile
raw_input("\n按 回车键 退出\n")
3. 配置文件名:config.ini如下:
代码如下:
#配置文件名:config.ini
[main]
sourceDir=wwwroot
txtFile=1.txt
targetDir=cp
希望本文所述对大家的Python程序设计有所帮助。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

数组的同质性对性能的影响是双重的:1)同质性允许编译器优化内存访问,提高性能;2)但限制了类型多样性,可能导致效率低下。总之,选择合适的数据结构至关重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,内存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版
中文版,非常好用

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

Atom编辑器mac版下载
最流行的的开源编辑器