[英]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.