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
16
votes
1 answer

How to synchronize two folders using python script

Currently, I am working on a project in which am synchronizing two folders. My folders in the following example names ad Folder_1 as source and Folder_2 as destination I want to do the following things. If files which are present in Folder_1 are…
Saqib Shakeel
  • 615
  • 2
  • 8
  • 17
15
votes
1 answer

Copy files to same directory with another name

I need to copy all html files inside the same directory with another name and I need to navigate all directories inside the source directory. Here is my code so far, import os import shutil os.chdir('/') dir_src = ("/home/winpc/test/copy/") for…
Kit
  • 273
  • 3
  • 5
  • 16
15
votes
2 answers

Is python's shutil.copyfile() atomic?

I'm writing a python script that copies a file using shutil.copyfile() on Linux. During the copying, other processes may be trying to read the file. Is the following sufficient to ensure that an external process doesn't get a corrupted view of the…
dinosaur
  • 3,164
  • 4
  • 28
  • 40
15
votes
1 answer

Permission denied doing os.mkdir(d) after running shutil.rmtree(d) in Python

Very often in the Windows 7 console if I run a python program twice very quickly that does if os.path.isdir(d): shutil.rmtree(d) if not os.path.exists(d): os.mkdir(d) where d is the name of a directory with many files, I get a "Permission…
mljrg
  • 4,430
  • 2
  • 36
  • 49
14
votes
2 answers

What is a complete list of Exceptions that can be thrown by shutil.rmtree

I am using the rmtree method from shutil in Python (2.7). What are all possible exceptions that can occur while calling this method?
Peter Smit
  • 27,696
  • 33
  • 111
  • 170
13
votes
6 answers

shutil.copytree without files

I'm trying use shutil.copytree: shutil.copytree(SOURCE_DIR, TARGET_DIR, ignore=None) This copy also files in folder. I need copy only folders without ANY files. How to do it?
user2216451
  • 139
  • 1
  • 6
12
votes
1 answer

Creating archive with shutil.make_archive() while excluding some path(s)

Is there a way to have shutil.make_archive exclude some child directories, or alternatively, is there a way to append directories to a single archive through shutil? Examples are fun: /root /workingDir /dir1 /dir2 /dirA …
MrDuk
  • 16,578
  • 18
  • 74
  • 133
11
votes
2 answers

Flask send_file is sending old file instead of newest

I have a flask app where using one Flask route the server creates a csv file and saves it to the server. Using a generated button on the client page, another Flask route is triggered to get the most recent file, move it to a tmp folder and send that…
Gugmi
  • 315
  • 1
  • 3
  • 9
11
votes
1 answer

Copy a file, but don't overwrite, without TOCTTOU issues in Python

I know that if I want to copy a file in Python but not overwrite the destination I can use code like this: if os.path.exists(dest): raise Exception("Destination file exists!") else: shutil.copy2(src, dest) But the state of the world could…
kuzzooroo
  • 6,788
  • 11
  • 46
  • 84
10
votes
2 answers

Checking when shutil.copyfile is done

I have a such code: for file in file_list: shutil.copyfile(file,newpath) #do further actions And here is the question, at #do further actions I use the copied f iles thus I need to make sure the shutil.copyfile functions finish their task. How…
Hellnar
  • 62,315
  • 79
  • 204
  • 279
9
votes
4 answers

Moving specific files in subdirectories into a directory - python

Im rather new to python but I have been attemping to learn the basics. Anyways I have several files that once i have extracted from their zip files (painfully slow process btw) produce several hundred subdirectories with 2-3 files in each. Now what…
GeoPy
  • 1,556
  • 3
  • 17
  • 21
9
votes
3 answers

Moving file to Recycle Bin

I'm trying to move file to recycle bin using shutil library. Following is relevant code lines, but I get kinda strange error. Both files are local, and I can access both locations on my PC. Why this error occurs? Because I run Main.py it from…
Matiss Zuravlevs
  • 329
  • 2
  • 11
9
votes
2 answers

Disable SameFileError exception in shutil.copy

I have the following code because sometimes these files will be the same and sometimes they won't. import shutil try: shutil.copy('filea','fileb') shutil.copy('filec','filed') except shutil.SameFileError pass The problem is that the second…
Ray Salemi
  • 5,247
  • 4
  • 30
  • 63
9
votes
3 answers

Overwrite directory with shutil.rmtree and os.mkdir sometimes gives 'Access is denied' error

My code: if os.path.exists(myDir): shutil.rmtree(myDir) os.mkdir(myDir) Problem: It always work if myDir does not exist. If myDir exists, sometimes it throws error, sometimes it works. Error log: os.mkdir(myDir) PermissionError: [WinError 5]…
TuTan
  • 150
  • 1
  • 7
9
votes
3 answers

Python: How to use shutil.make_archive?

I can't figure out how to use shutil.make_archive to zip a folder into a zip-file and then put that saved_20170721.zip file into the folder named past_data I have the code: from shutil import make_archive from datetime import…
user1367204
  • 4,549
  • 10
  • 49
  • 78
1
2
3
78 79