【问题标题】:os.walk one level downos.walk 下一层
【发布时间】:2018-07-10 04:33:02
【问题描述】:

我有一个结构如下的文件夹 (A):

A\b
A\c
A\d

我想像这样将文件夹 b、c 和 d 向下复制 3 次:

A\b\b1\b2\b3
A\c\c1\c2\c3
A\d\d1\d2\d3

其中 b1、b2 和 b3 是文件夹 b 的副本,c 和 d 也是如此。

当我运行下面的代码时,我最终得到了所需的结构,但还在每个子文件夹中添加了三个副本,因此 A\b\b1 具有 b1、b2 和 b3 文件夹。问题与循环有关,但我无法确定它

import os
import sys
import shutil

list1= [1,2,3]
path = "C:\ladg\oner"
root, dirs, files = os.walk(path).next()

for dir in dirs:
    for i in list1:
        new_folder_path = os.path.join(root,dir, dir+ str(i))
        shutil.copytree(os.path.join(root, dir), new_folder_path)

【问题讨论】:

  • 将原始目录(例如b)复制到目录tmp_b(具有唯一名称)。将其复制到b1b1\b2,然后将临时副本移动到b1\b2\b3

标签: python shutil os.walk python-os


【解决方案1】:

试试这个。您当前正在遍历列表而不更改目录。

import os
import sys
import shutil

list1= [1,2,3]
path = "C:\ladg\oner"
root, dirs, files = os.walk(path).next()

for dir in dirs:
    curr_path = os.path.join(root, dir)
    for i in list1:
        new_folder_path = os.path.join(curr_path, dir + str(i))
        shutil.copytree(curr_path, new_folder_path)
        os.chdir(new_folder_path)
        curr_path = new_folder_path

在后续问题中,如果您想实现拥有此结果目录的场景:

A
-- b
   -- b1
   -- b2 
   -- b3
-- c
   -- c1
   -- c2
   -- c3

您可以使用以下代码。这与第一个建议不同,因为现在您要创建一个tmp_dir 来存储您的副本,然后最终用这个新的tmp_dir 替换旧的curr_dir。当您进行后续复制时,这对于维护curr_dir 的完整副本是必要的。

import os
import sys
import shutil

list1= [1,2,3]
path = "~/A"
root, dirs, files = os.walk(path).next()

for dir in dirs:
    curr_dir = os.path.join(root, dir)
    tmp_dir = os.path.join(root, "tmp_" + dir)
    os.mkdir(tmp_dir)
    for i in list1:
        new_folder_path = os.path.join(tmp_dir, dir+ str(i))
        shutil.copytree(curr_dir, new_folder_path)
    shutil.rmtree(curr_dir)
    shutil.move(tmp_dir, curr_dir)

【讨论】:

  • 当我在 Python 2 中运行它时,它在 copytree 行上崩溃,说 OSError: [Errno 2] No such file or directory: 'A/b/b1'
  • 您可以尝试更改路径以包含目录 A 吗?例如在 linux 中是 /ladg/oner/A
  • 我在评论之前做过。
  • 也许这与os.path.join 与 linux for windows 的工作方式不同有关。您可以查看这篇文章stackoverflow.com/questions/2422798/…,还可以查看运行shutil.copytree时新文件夹的创建位置@
  • @lamaee201 我刚刚用第二种解决方案修改了我的答案,可以满足您的需求
【解决方案2】:

基本策略是将原始目录(例如b)复制到目录tmp_b(具有唯一名称)。将其复制到b1b1\b2,然后将临时副本移动到b1\b2\b3

以下是一些实现此功能的代码:

#!/usr/bin/env python3
import os, sys, shutil
import tempfile

def make_nested_copies(path, N):
    root, dirs, _ = next(os.walk(path))
    for current_dir_name in dirs:
        temp = tempfile.mkdtemp()
        temp_path = os.path.join(temp, current_dir_name)

        destination_path = os.path.join(root, current_dir_name)
        shutil.copytree(destination_path, temp_path)

        for i in range(1,N):
            destination_path = os.path.join(destination_path, current_dir_name + str(i))
            shutil.copytree(temp_path, destination_path)

        destination_path = os.path.join(destination_path, current_dir_name + str(N))
        shutil.move(temp_path, destination_path)

if __name__ == '__main__':
    make_nested_copies(sys.argv[1], 3)

使用以下目录结构进行测试:

A
|-- A.txt
|-- b
|   `-- b.txt
|-- c
|   `-- c.txt
`-- d
    `-- d.txt

结果:

A
|-- A.txt
|-- b
|   |-- b.txt
|   `-- b1
|       |-- b.txt
|       `-- b2
|           |-- b.txt
|           `-- b3
|               `-- b.txt
|-- c
|   |-- c.txt
|   `-- c1
|       |-- c.txt
|       `-- c2
|           |-- c.txt
|           `-- c3
|               `-- c.txt
`-- d
    |-- d.txt
    `-- d1
        |-- d.txt
        `-- d2
            |-- d.txt
            `-- d3
                `-- d.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 2020-05-13
    • 1970-01-01
    相关资源
    最近更新 更多