简体   繁体   English

如何在python中复制文件夹结构的第一层?

[英]How do I copy the first layer of a folder structure in python?

Consider following folder structure考虑以下文件夹结构

├─a
│  ├─a1
│  ├─a2
|      ├─a2_1
│      ├─a2_2
│  └─a3
│      ├─a3_1
│      ├─a3_2
│      └─a3_3

Is there any way to copy the folders a1, a2, a3 without any subfolders or files into a folder b so it would look like this:有没有办法将没有任何子文件夹或文件的文件夹 a1、a2、a3 复制到文件夹 b 中,所以它看起来像这样:

├─b
│  ├─a1
│  ├─a2
|  └─a3

If you don't want to copy subdirectories or files, you aren't really performing any copy at all.如果您不想复制子目录或文件,那么您根本没有真正执行任何复制。 All you need to do is to create directories with same names as in the input folder.您需要做的就是创建与输入文件夹中名称相同的目录。

import os

adir = 'a'
bdir = 'b'

for fn in os.listdir(adir):
    if os.path.isdir(os.path.join(adir, fn)):
      os.mkdir(os.path.join(bdir, fn))

This should work这应该工作

import os

for name in os.listdir("<PATH_TO_A>"):
    if os.path.isdir("<PATH_TO_A>" + name):
        os.mkdir("<PATH_TO_B>" + name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 的文件夹结构中找到文件并复制文件所在的整个文件夹? - How can i find a file in a folder structure in python and copy the whole folder where the file is? 如何将文件复制到python中的只读文件夹 - How do i copy files to a read-only folder in python 如何将一种特定类型的所有文件从一个文件夹复制到Python中的另一个文件夹 - How do I copy all files of one specific type from a folder to another folder in Python 如何在不复制python中的文件夹结构的同时从文件夹(包括子文件夹)复制所有文件 - How to copy all files from a folder (including sub-folder) while not copying the folder structure in python 使用具有完整文件夹结构的 python 复制文件 - Copy files using python with complete folder structure Python ggplot-如何分层直方图 - Python ggplot - how do I layer histograms 如何导入 Python lambda 层? - How do I import a Python lambda layer? 如何打开文件夹中的第一个文件 - How do I open the first file in a folder Python-图形数据结构-如何实施广度优先搜索以正确找到可到达的顶点 - Python - Graph Data Structure - How Do I Implement Breadth-First Search to Find Reachable Vertices Correctly 如何使用 python 将几个文件夹从一个目录复制到 Linux 中的另一个文件夹 - how do I copy over a few folders from one directory into another folder in Linux using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM