python文件夹合并_Python 合并两个文件夹

匿名用户

1级

2014-12-15 回答

这个用不着python吧,

在windows下直接 Xcopy B A  /s /e /y

在linux下 cp -r A B

但是还是给你提供python的方法吧

#!/usr/bin/env python

# -*- coding: utf-8 -*-

"""

# ------------------------------------------------------------------------

# FileName:     [file_dir_copy.py]

# Purpose:      [copy dir B to A]

# ------------------------------------------------------------------------

"""

# import necessary module

import os

import shutil

from os.path import walk

oj = os.path.join

oif = os.path.isfile

oid = os.path.isdir

PathA = "D:\\PathA\\"

PathB = "D:\\PathB\\"

# ============================================================

def copy(arg, dirname, filenames):

"""

Purpose / Usage: copy() is a func to create dir and copy file

from PathB to PathA by recursion

Parameter(s):@arg, @dirname, @filenames

"""

# for showing progress

print dirname

# remove root dir

diretory = dirname.replace(PathB, "")

dirnameA = os.path.join(PathA, diretory)

if oid(dirnameA):

# if there is a dir in PathA then check

# if subdirs and files are existing.

for FILE in filenames:

if oif(oj(dirname, FILE)) and not oif(oj(dirnameA, FILE)):

# if not exists files then copy files to pathA

shutil.copy2(oj(dirname, FILE), oj(dirnameA, FILE))

elif oid(oj(dirname, FILE)) and not oid(oj(dirnameA, FILE)):

# if not exsits dir then make dir in PathA

os.system("mkdir %s" % (oj(dirnameA, FILE)))

else:

# if there is no same dir, then create the dir in PatchA,

# and copy files

os.system("mkdir %s" % (dirnameA))

for FILE in filenames:

shutil.copy2(oj(dirname, FILE), oj(dirnameA, FILE))

# shutil.copy2 func can copy with original date

# and time of file.

# ============================================================

if __name__ == "__main__":

walk(PathB, copy, ())

# call copy func recursively

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值