Questions tagged [shutil]

A Python module which contains a number of utility methods for file or directory operations, such as copying, moving, etc.

The Python shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal.

Documentation: https://docs.python.org/3.2/library/shutil.html

1181 questions
363
votes
15 answers

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

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…
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
129
votes
4 answers

Copy directory contents into a directory with python

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…
prosseek
  • 182,215
  • 215
  • 566
  • 871
91
votes
5 answers

shutil.rmtree fails on Windows with 'Access is denied'

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…
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
78
votes
2 answers

Move and replace if same file name already exists?

Here is below code which will move and replace individual file: import shutil import os src = 'scrFolder' dst = './dstFolder/' filelist = [] files = os.listdir( src ) for filename in files: filelist.append(filename) fullpath = src + '/' +…
user1891916
  • 951
  • 1
  • 8
  • 9
78
votes
3 answers

python copy files by wildcards

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…
Johnny
  • 789
  • 1
  • 5
  • 3
67
votes
8 answers

Compressing directory using shutil.make_archive() while preserving directory structure

I'm trying to zip a directory called test_dicoms to a zip file named test_dicoms.zip using the following code: shutil.make_archive('/home/code/test_dicoms', 'zip', '/home/code/test_dicoms') The problem is that when I unzip it, all of the files that…
G Warner
  • 1,309
  • 1
  • 15
  • 27
58
votes
1 answer

Python Pathlib path object not converting to string

I am trying to use Shutil to copy a pdf file using path objects from Pathlib, however when I run my code I get the error "str object is not callable" when converting my paths back to strings using str(). Any explanation for why this is occurring…
MrClean
  • 1,300
  • 2
  • 12
  • 17
57
votes
4 answers

shutil.rmtree() clarification

I have read the documentation for this function, however, I dont think I understand it properly. If anyone can tell me what I'm missing, or if I am correct, it would be a great help. Here is my understanding: using the shutil.rmtree(path) function,…
IT Ninja
  • 6,174
  • 10
  • 42
  • 65
30
votes
4 answers

How to Copy Files Fast

What is the fastest way to copy files in a python program? It takes at least 3 times longer to copy files with shutil.copyfile() versus to a regular right-click-copy > right-click-paste using Windows File Explorer or Mac's Finder. Is there any…
alphanumeric
  • 17,967
  • 64
  • 244
  • 392
24
votes
3 answers

using shutil.copyfile I get a Python IOError: [Errno 13] Permission denied:

I have some python code using shutil.copyfile: import os import shutil src='C:\Documents and Settings\user\Desktop\FilesPy' des='C:\Documents and…
DrDark
  • 445
  • 2
  • 4
  • 12
22
votes
1 answer

what does shutil (in Python) mean?

I knew what this module is for, I just don't know why they choose this name - shutil. It is hard to remember this kind of "random" name if I don't know where it come from. Please give me some hints, thanks.
Justin
  • 233
  • 2
  • 4
20
votes
5 answers

Filter directory when using shutil.copytree?

Is there a way I can filter a directory by using the absolute path to it? shutil.copytree(directory, target_dir, ignore = shutil.ignore_patterns("/Full/Path/To/aDir/Common")) This doesn't seem to work when trying to…
Goles
  • 11,599
  • 22
  • 79
  • 140
20
votes
3 answers

shutil.rmtree to remove readonly files

I want to use shutil.rmtree in Python to remove a directory. The directory in question contains a .git control directory, which git marks as read-only and hidden. The read-only flag causes rmtree to fail. In Powershell, I would do "del -force" to…
Paul Moore
  • 6,569
  • 6
  • 40
  • 47
18
votes
3 answers

Errno 2 using python shutil.py No such file or directory for file destination

I am using the shutil python module to copy files and directories on a linux redhat machine. I wrote the following method, which takes in 2 params: src (the path of the file or dir that is being collected) and destination (the desired new path of…
OMGitzMidgar
  • 689
  • 2
  • 10
  • 28
18
votes
3 answers

Why is shutil.copytree not copying tree from source to destination?

I have a function: def path_clone( source_dir_prompt, destination_dir_prompt) : try: shutil.copytree(source_dir_prompt, destination_dir_prompt) print("Potentially copied?") except OSError as e: # If the error was…
CodeTalk
  • 3,571
  • 16
  • 57
  • 92
1
2 3
78 79