简体   繁体   English

如何使用 python 将几个文件夹从一个目录复制到 Linux 中的另一个文件夹

[英]how do I copy over a few folders from one directory into another folder in Linux using python

I'm trying to copy over a bunch of folders, read from a txt file into another folder.我正在尝试复制一堆文件夹,从一个 txt 文件读取到另一个文件夹中。 How do I do this?我该怎么做呢? I'm using python in Linux我在 Linux 中使用 python

eg this txt file has the following folder names例如,此 txt 文件具有以下文件夹名称

001YG  
00HFP  
00MFE  
00N38  
00NN7  
00SL4  
00T1E  
00T4B  
00X3U  
00YZL  
00ZCA  
01K8X  
01KM1  
01KML  
01O27  
01THT  
01ZWG  

and I want to copy these folders and their contents to a folder called duplicate-folder我想将这些文件夹及其内容复制到一个名为duplicate-folder

How do I do this?我该怎么做呢? I tried the following code but it gave an error because I think it's only for files, not folders:)我尝试了以下代码,但它给出了一个错误,因为我认为它只适用于文件,而不适用于文件夹:)

with open("missing-list-files.txt",'w') as outfile:
    # outfile.write('\n'.join(temp3))
    for x in temp3:
        outfile.write(x + '\n')

    filelistset = temp3     
    dest = 'mydirectoryhere'
    for file in filelistset:
        shutil.copyfile(file ,dest + file)

use shutil.copytree(src, dest, ...) ( https://docs.python.org/3/library/shutil.html#shutil.copytree ) for directories (also copies subfiles and -directories. And also don't forget to give it the full path for file and dest (don't know which value your temp3 var holds). If you don't give it a full path, but only a filename, it will search in the current working directory.shutil.copytree(src, dest, ...) ( https://docs.python.org/3/library/shutil.html#shutil.copytree ) 用于目录(也复制子文件和目录。也不要'不要忘记给它提供文件和目标的完整路径(不知道你的 temp3 变量持有哪个值)。如果你不给它一个完整路径,而只给它一个文件名,它将在当前工作目录中搜索。

暂无
暂无

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

相关问题 如何根据文件夹名称将文件从一个带有子文件夹的目录复制到另一个带有子文件夹的目录? - How do I copy files from one directory with sub-folders to another directory with sub-folders based on folder name? Python - 将文件夹从一个目录复制到另一个目录(仅复制我包含在 txt 文件中的文件夹) - Python - Copy folders from one directory to another directory (Copy only that folders that i have included in a txt files) 如何使用 Python 将 zipfile 从一个文件夹复制到另一个文件夹 - How to copy zipfile from one folder to another folder using Python 如何将一种特定类型的所有文件从一个文件夹复制到Python中的另一个文件夹 - How do I copy all files of one specific type from a folder to another folder in Python 如何将文件夹从 gcp 存储桶文件夹复制到另一个存储桶文件夹并使用 python 将其删除 - how to copy folders from a gcp bucket folder into another bucket folder and delete it using python 如何在 Python 中将所有文件夹内容从一个位置复制到另一个位置? - How do I copy all folder contents from one location to another location in Python? 如何使用 Python 将图像从一个文件夹复制到另一个文件夹? - How to copy images from one folder to another using Python? Python如何将所有文件夹内容从一个位置复制到另一个位置? - How do I copy all folder contents from one location to another location in Python? 如何使用python将多个文件夹中的多个文件复制到一个文件夹中? - How to copy multiple files from multiple folders into one folder using python? 将文件夹目录中的所有.csv文件复制到python中的一个文件夹 - Copy all .csv files in a directory of folders to one folder in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM