shutil

python copy files by wildcards

ぃ、小莉子 提交于 2019-11-27 07:45:08
I am learning python (python 3) and I can copy 1 file to a new directory by doing this import shutil shutil.copyfile('C:/test/test.txt', 'C:/lol/test.txt') What I am now trying to do is to copy all *.txt files from C:/ to C:/test *.txt is a wildcard to search for all the text files on my hard drive jseanj import glob import shutil dest_dir = "C:/test" for file in glob.glob(r'C:/*.txt'): print(file) shutil.copy(file, dest_dir) Use glob.glob() to get a list of the matching filenames and then iterate over the list. I am using python 2.7 test first to make sure it will work. I used the wildcard *

shutil.rmtree fails on Windows with 'Access is denied' [duplicate]

烈酒焚心 提交于 2019-11-27 03:00:43
This question already has an answer here: Deleting directory in Python 6 answers In Python, when running shutil.rmtree over a folder that contains a read-only file, the following exception is printed: File "C:\Python26\lib\shutil.py", line 216, in rmtree rmtree(fullname, ignore_errors, onerror) File "C:\Python26\lib\shutil.py", line 216, in rmtree rmtree(fullname, ignore_errors, onerror) File "C:\Python26\lib\shutil.py", line 216, in rmtree rmtree(fullname, ignore_errors, onerror) File "C:\Python26\lib\shutil.py", line 216, in rmtree rmtree(fullname, ignore_errors, onerror) File "C:\Python26

Copy directory contents into a directory with python [duplicate]

和自甴很熟 提交于 2019-11-26 19:01:17
问题 This question already has answers here : How do I copy an entire directory of files into an existing directory using Python? (14 answers) Closed 12 months ago . I have a directory /a/b/c that has files and subdirectories. I need to copy the /a/b/c/* in the /x/y/z directory. What python methods can I use? I tried shutil.copytree("a/b/c", "/x/y/z") , but python tries to create /x/y/z and raises an error "Directory exists" . 回答1: I found this code working. from distutils.dir_util import copy

python copy files by wildcards

纵然是瞬间 提交于 2019-11-26 13:48:05
问题 I am learning python (python 3) and I can copy 1 file to a new directory by doing this import shutil shutil.copyfile('C:/test/test.txt', 'C:/lol/test.txt') What I am now trying to do is to copy all *.txt files from C:/ to C:/test *.txt is a wildcard to search for all the text files on my hard drive 回答1: import glob import shutil dest_dir = "C:/test" for file in glob.glob(r'C:/*.txt'): print(file) shutil.copy(file, dest_dir) 回答2: Use glob.glob() to get a list of the matching filenames and then

How do I copy an entire directory of files into an existing directory using Python?

旧巷老猫 提交于 2019-11-26 12:46:00
Run the following code from a directory that contains a directory named bar (containing one or more files) and a directory named baz (also containing one or more files). Make sure there is not a directory named foo . import shutil shutil.copytree('bar', 'foo') shutil.copytree('baz', 'foo') It will fail with: $ python copytree_test.py Traceback (most recent call last): File "copytree_test.py", line 5, in <module> shutil.copytree('baz', 'foo') File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/shutil.py", line 110, in copytree File "/System/Library/Frameworks/Python