繁体   English   中英

Python - 将文件夹从一个目录复制到另一个目录(仅复制我包含在 txt 文件中的文件夹)

[英]Python - Copy folders from one directory to another directory (Copy only that folders that i have included in a txt files)

我必须将文件夹从一个目录复制到另一个目录(仅复制我包含在 txt 文件中的文件夹)

我可以复制所有文件夹,但如何只复制特定文件夹? 例如:我想复制以 111 开头的文件夹,所以它应该只复制以 111 开头的文件夹。所以基本上,我必须在 txt 文件中包含某些文件夹名称,并在我的 python 代码中使用该文本文件,以便它只会复制txt文件中包含的文件夹。

from distutils.dir_util import copy_tree

fromDirectory = "/a/b/c"

toDirectory = "/x/y/z"

copy_tree(fromDirectory, toDirectory)

您可以使用glob来获取带有您感兴趣的特定标记的文件:

import glob
import os

files = glob.glob('path/to/dir/111**')

for f in files:
    os.rename(f, os.path.join('path/to/new/dir', os.path.split(f)[1]))

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM