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

How to shutil.copyfile only if file differ?

shutil.copyfile is quite useful for copying files from a location to another. Unfortunately, it does copy the file even though it already exists. I find rsync --checksum quite convenient in this case, but I don't think it worth calling rsync from…
nowox
  • 25,978
  • 39
  • 143
  • 293
9
votes
2 answers

Python Shutil.copy if I have a duplicate file will it copy to new location

I'm working with the shutil.copy method in python. I found the definition listed below: def copyFile(src, dest): try: shutil.copy(src, dest) # eg. src and dest are the same file except shutil.Error as e: print('Error: %s'…
Jack Walker
  • 238
  • 1
  • 2
  • 10
8
votes
4 answers

How to write a call back function for ignore in shutil.copytree

I am relatively new to python. I am trying to copy a directory to another directory maintaining the structure. I am using shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False) I am…
user2434
  • 6,339
  • 18
  • 63
  • 87
8
votes
1 answer

Python (openpyxl) : Put data from one excel file to another (template file) & save it with another name while retaining the template

I have a template excel file named as template.xlsx which has a number of sheets. I would like to copy data from a seperate .csv file into the first sheet of template.xlsx (named as data) and save the new file as result.xlsx while retaining the…
Arpit Sharma
  • 345
  • 6
  • 15
8
votes
3 answers

Python shutil copyfile - missing last few lines

I am routinely missing the last few kb of a file I am trying to copy using shutil copyfile. I did some research and do see someone asking about something similar here: python shutil copy function missing last few lines But I am using copyfile,…
10mjg
  • 573
  • 1
  • 6
  • 18
8
votes
4 answers

How do I create a zip file of a file path using Python, including empty directories?

I've been trying to use the zipfile and shutil.make_archive modules to recursively create a zip file of a directory. Both modules work great--except empty directories do not get added to the archive. Empty directories containing other empty…
jamieb
  • 9,847
  • 14
  • 48
  • 63
7
votes
2 answers

shutil.make_archive not zipping to correct destination

As per the code below I am having issues with the zipping a directory using the python 3 shutil.make_archive function. The .testdir will be zipped but it is being zipped in /home/pi, instead of /home/pi/Backups. zip_loc =…
somerandomguy95
  • 161
  • 1
  • 3
  • 13
7
votes
1 answer

Is there a foolproof way to give the system enough time to delete a folder before running copytree

I have some directories that I need to update I was using the following code for newdir in newdirs: olddir = newdir.replace('h:\\','G:\\').replace('_academic','') shutil.rmtree(newdir) shutil.copytree(olddir,newdir) I would occasionally…
PyNEwbie
  • 4,882
  • 4
  • 38
  • 86
7
votes
5 answers

Seeming discrepancy in shutil.disk_usage()

I am using the shutil.disk_usage() function to find the current disk usage of a particular path (amount available, used, etc.). As far as I can find, this is a wrapper around os.statvfs() calls. I'm finding that it is not giving the answers I'd…
Joel Wigton
  • 642
  • 6
  • 15
6
votes
0 answers

Is there some sort of way to track the progress of shutil.make_archive()?

I am making a script that compresses folders via shutil.make_archive, but the folders can be large, so it will take a while. Is there some sort of way I can show the percentage complete? So far I have been checking out the threading module, but I…
TheRealTengri
  • 1,201
  • 1
  • 7
  • 17
6
votes
1 answer

shutil.move() only works with existing folder?

I would like to use the shutil.move() function to move some files which match a certain pattern to a newly created(inside python script)folder, but it seems that this function only works with existing folders. For example, I have 'a.txt', 'b.txt',…
user9875189
  • 179
  • 1
  • 2
  • 10
6
votes
7 answers

How to copy only the changed file-contents on the already existed destination file?

I have a script which i'm using for copy purpose from one location to another location and the file beneath the directory structure are all .txt files. This script just evaluates the file size on the source and only copy if the file-size is not…
krock1516
  • 441
  • 10
  • 30
6
votes
2 answers

Is there a way to interrupt shutil copytree operation in Python?

I'm fairly new to programming in general. I need to develop a program that can copy multiple directories at once and also take into account multiple file type exceptions. I came across the shutil module which offers the copytree and ignore_patterns…
6
votes
3 answers

Python - copying specific files from a list into a new folder

I am trying to get my program to read a list of names from a file (say .txt), then search for those in a selected folder and copy and paste those files to another selected folder. My program runs without errors but does not do anything: Code -…
JasonDL
  • 127
  • 1
  • 2
  • 12
6
votes
2 answers

How can I make my python script wait for a shutil.move to be actually completed?

A python script I'm using is at one point moving a list of files from one directory to the other. I have implemented it with shutil.move: for a_file in list_of_filenames: src = os.path.join(src_dir, a_file) dest = os.path.join(dest_dir,…
Valentin B.
  • 602
  • 6
  • 18
1 2
3
78 79