zoukankan      html  css  js  c++  java
  • Python 文件拷贝

    最近在备份手机上的照片的时候,纯手工操作觉得有些麻烦,就想写个脚本自动进行。因为备份的时候有些照片以前备份过了,所以需要有个判重操作。

    主要功能在copyFiles()函数里实现,如下:

     1 def copyFiles(src, dst):
     2     srcFiles = os.listdir(src)
     3     dstFiles = dict(map(lambda x:[x, ''], os.listdir(dst)))
     4     filesCopiedNum = 0
     5     
     6     # 对源文件夹中的每个文件若不存在于目的文件夹则复制
     7     for file in srcFiles:
     8         src_path = os.path.join(src, file)
     9         dst_path = os.path.join(dst, file)
    10         # 若源路径为文件夹,若存在于目标文件夹,则递归调用本函数;否则先创建再递归。
    11         if os.path.isdir(src_path):
    12             if not os.path.isdir(dst_path):
    13                 os.makedirs(dst_path)  
    14             filesCopiedNum += copyFiles(src_path, dst_path)
    15         # 若源路径为文件,不重复则复制,否则无操作。
    16         elif os.path.isfile(src_path):                
    17             if not dstFiles.has_key(file):
    18                 shutil.copyfile(src_path, dst_path)
    19                 filesCopiedNum += 1
    20             
    21     return filesCopiedNum

     这里我首先使用os.listdir()函数来遍历源文件夹src和目标文件夹dst,得到两个文件列表,但由于我需要判重操作,因此需要在dst文件列表中进行查询操作。由于列表的查询效率不高,而字典是一个哈希表,查询效率较高,因此我将目标文件列表转换成一个只有键没有值的字典:

    dstFiles = dict(map(lambda x:[x, ''], os.listdir(dst)))

    然后我遍历源文件列表,若该路径是一个文件夹,先判断该文件夹在目标路径中是否存在,若不存在,则先创建一个新路径。然后递归调用本函数。其实不存在的时候更高效的方法是调用shutil.copytree()函数,但由于此处需要计算拷贝的文件数量,因此就没有调用该函数。

    若该路径是一个文件,则首先判断该文件在目标文件夹中是否存在。若不存在,则拷贝。

    由于写这个脚本主要是为了同步手机相册到PC,因此只简单地判断一下文件名。若要判断不同名但相同的文件,则可以继续判断一下md5值,这里就不再赘述。

    完整代码如下:

     1 #!/usr/bin/env python
     2 # -*- coding: UTF-8 -*-
     3 
     4 # 输入两个文件夹a和b路径,将a中的文件拷进b,并计算拷贝的文件数。重复的不作处理。
     5 
     6 import os
     7 import shutil
     8 
     9 def copyFiles(src, dst):
    10     srcFiles = os.listdir(src)
    11     dstFiles = dict(map(lambda x:[x, ''], os.listdir(dst)))
    12     filesCopiedNum = 0
    13     
    14     # 对源文件夹中的每个文件若不存在于目的文件夹则复制
    15     for file in srcFiles:
    16         src_path = os.path.join(src, file)
    17         dst_path = os.path.join(dst, file)
    18         # 若源路径为文件夹,若存在于目标文件夹,则递归调用本函数;否则先创建再递归。
    19         if os.path.isdir(src_path):
    20             if not os.path.isdir(dst_path):
    21                 os.makedirs(dst_path)  
    22             filesCopiedNum += copyFiles(src_path, dst_path)
    23         # 若源路径为文件,不重复则复制,否则无操作。
    24         elif os.path.isfile(src_path):                
    25             if not dstFiles.has_key(file):
    26                 shutil.copyfile(src_path, dst_path)
    27                 filesCopiedNum += 1
    28             
    29     return filesCopiedNum
    30 
    31 def test():
    32     src_dir = os.path.abspath(raw_input('Please enter the source path: '))
    33     if not os.path.isdir(src_dir):
    34         print 'Error: source folder does not exist!'
    35         return 0
    36     
    37     dst_dir = os.path.abspath(raw_input('Please enter the destination path: '))
    38     if os.path.isdir(dst_dir):
    39         num = copyFiles(src_dir, dst_dir)
    40     else:
    41         print 'Destination folder does not exist, a new one will be created.'
    42         os.makedirs(dst_dir)
    43         num = copyFiles(src_dir, dst_dir)
    44 
    45     print 'Copy complete:', num, 'files copied.'
    46 
    47 if __name__ == '__main__':
    48     test()
  • 相关阅读:
    oracle update join
    ResultSet转换为List的方法
    oracle创建用户操作
    ArcGIS打开工具无响应
    当前不会命中断点。源代码与原始版本不同
    使用Skin Editor自定义主题皮肤
    Spring boot项目设置加载静态资源的路径(spring.resources.static-locations)
    linux下文件的复制、移动与删除
    【珍藏】清华大学状元笔记 (初中+高中)全套高清笔记电子版 PDF
    【珍藏】全付费 (共204本)图灵程序设计丛书 高清电子书 带书签 PDF
  • 原文地址:https://www.cnblogs.com/dukeleo/p/3199157.html
Copyright © 2011-2022 走看看