在python中合并两个文件夹

时间:2018-03-06 02:46:34

标签: python merge

我需要合并两个文件夹,

文件夹名为12345和12345 _

我如何合并这两个?

我已经尝试了,但我最终得到了' 12345。'

for file in files:
            subFolder = os.path.join(destpath, file[:6])
            if not os.path.isdir(subFolder):
                os.makedirs(subFolder)
            shutil.copy(os.path.join(root, file), subFolder)

3 个答案:

答案 0 :(得分:0)

你可以使用这样的东西,它将文件夹1中的所有文件复制到文件夹2,因此文件夹2将包含来自1和2的所有文件:

#!/usr/bin/env python

import subprocess as sbp
import os

path=raw_input('Please enter a path\n')
fol = os.listdir(path)
p2 = raw_input('Please enter a path\n')

for i in fol:
    p1 = os.path.join(path,i)
    p3 = 'cp -r ' + p1 +' ' + p2+'/.'
    sbp.Popen(p3,shell=True)

答案 1 :(得分:0)

从 python 中,您也可以调用 rsync,这是为此而设计的。

import subprocess

## define your paths
path1 = '/path/to/12345/'
path2 = '/path/to/12345_/'

## where to place the merged data
merged_path = '/path/to/merged/'

## write an rsync commands to merge the directories
rsync_cmd = 'rsync' + ' -avzh ' + path1 + ' ' + path2 + ' ' + merged_path

## run the rsync command
subprocess.run(rsync_cmd, shell=True)

答案 2 :(得分:-4)

写入Excel工作表的这个选项怎么样?

import xlsxwriter


# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('C:/Users/your_path_here/Excel_File.xlsx')
worksheet = workbook.add_worksheet()

# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)

# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})

# Write some simple text.
worksheet.write('A1', 'Hello')

# Text with formatting.
worksheet.write('A2', 'World', bold)

# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)


workbook.close()