python复制文件代码实现
主要功能在copyFiles()函数里实现,如下:
def copyFiles(src, dst):
srcFiles = os.listdir(src)
dstFiles = dict(map(lambda x:[x, ''], os.listdir(dst)))
filesCopiedNum = 0
# 对源文件夹中的每个文件若不存在于目的文件夹则复制
for file in srcFiles:
src_path = os.path.join(src, file)
dst_path = os.path.join(dst, file)
# 若源路径为文件夹,若存在于目标文件夹,则递归调用本函数;否则先创建再递归。
if os.path.isdir(src_path):
if not os.path.isdir(dst_path):
os.makedirs(dst_path)
filesCopiedNum += copyFiles(src_path, dst_path)
# 若源路径为文件,不重复则复制,否则无操作。
elif os.path.isfile(src_path):
if not dstFiles.has_key(file):
shutil.copyfile(src_path, dst_path)
filesCopiedNum += 1
return filesCopiedNum
这里我首先使用os.listdir()函数来遍历源文件夹src和目标文件夹dst,得到两个文件列表,但由于我需要判重操作,因此需要在dst文件列表中进行查询操作。由于列表的查询效率不高,而字典是一个哈希表,查询效率较高,因此我将目标文件列表转换成一个只有键没有值的字典:
dstFiles = dict(map(lambda x:[x, ''], os.listdir(dst)))
然后我遍历源文件列表,若该路径是一个文件夹,先判断该文件夹在目标路径中是否存在,若不存在,则先创建一个新路径。然后递归调用本函数。其实不存在的时候更高效的方法是调用shutil.copytree()函数,但由于此处需要计算拷贝的文件数量,因此就没有调用该函数。
若该路径是一个文件,则首先判断该文件在目标文件夹中是否存在。若不存在,则拷贝。
由于写这个脚本主要是为了同步手机相册到PC,因此只简单地判断一下文件名。若要判断不同名但相同的文件,则可以继续判断一下md5值,这里就不再赘述。
完整代码如下:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# 输入两个文件夹a和b路径,将a中的文件拷进b,并计算拷贝的文件数。重复的不作处理。
import os
import shutil
def copyFiles(src, dst):
srcFiles = os.listdir(src)
dstFiles = dict(map(lambda x:[x, ''], os.listdir(dst)))
filesCopiedNum = 0
# 对源文件夹中的每个文件若不存在于目的文件夹则复制
for file in srcFiles:
src_path = os.path.join(src, file)
dst_path = os.path.join(dst, file)
# 若源路径为文件夹,若存在于目标文件夹,则递归调用本函数;否则先创建再递归。
if os.path.isdir(src_path):
if not os.path.isdir(dst_path):
os.makedirs(dst_path)
filesCopiedNum += copyFiles(src_path, dst_path)
# 若源路径为文件,不重复则复制,否则无操作。
elif os.path.isfile(src_path):
if not dstFiles.has_key(file):
shutil.copyfile(src_path, dst_path)
filesCopiedNum += 1
return filesCopiedNum
def test():
src_dir = os.path.abspath(raw_input('Please enter the source path: '))
if not os.path.isdir(src_dir):
print 'Error: source folder does not exist!'
return 0
dst_dir = os.path.abspath(raw_input('Please enter the destination path: '))
if os.path.isdir(dst_dir):
num = copyFiles(src_dir, dst_dir)
else:
print 'Destination folder does not exist, a new one will be created.'
os.makedirs(dst_dir)
num = copyFiles(src_dir, dst_dir)
print 'Copy complete:', num, 'files copied.'
if __name__ == '__main__':
test()

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途

在Python中,如何通过字符串动态创建对象并调用其方法?这是一个常见的编程需求,尤其在需要根据配置或运行...
