简体   繁体   中英

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

I have to copy folders from one directory to another directory (Copy only that folders that I have included in a txt file)

I am able to copy all folders but how to copy only specific folders? Eg: I want to copy folder that starts with 111, so it should only copy the folder that starts with 111. So basically, I have to include certain folder names in a txt file and use that text file in my python code so that it would only copy the folders which are included in the txt file.

from distutils.dir_util import copy_tree

fromDirectory = "/a/b/c"

toDirectory = "/x/y/z"

copy_tree(fromDirectory, toDirectory)

You could use glob so to grab the files with the specific marker you're interested in:

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]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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