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
5
votes
2 answers

python copying the files with include pattern

I need to copy the files with include pattern using python script. Since shutil supports ignore_patterns to ignore the file. Is there any method to include the pattern to copy the files. Otherwise do I have to write the code explicitly?. Thanks in…
ArockiaRaj
  • 588
  • 4
  • 17
5
votes
1 answer

Python getting WindowsError 5 when deleting a file even though I have full permissions

quick question about Python on windows. I have a script that compiles a program (using an install rule), and then moves the build products to a remote destination over the network. However, I keep getting WindowsError 5 Access Denied. All files are…
shayst
  • 267
  • 4
  • 14
5
votes
2 answers

`shutil.rmtree` does not work on `tempfile.TemporaryDirectory()`

Consider this test import shutil, tempfile from os import path import unittest from pathlib import Path class TestExample(unittest.TestCase): def setUp(self): # Create a temporary directory self.test_dir =…
bluesmonk
  • 1,237
  • 13
  • 31
5
votes
1 answer

Python: How to move list of folders (with subfolders) to a new directory

I have a directory that literally has 3000+ folders (all with subfolders). What I need to be able to do is to look through the directory for those files and move them to another folder. I've done some research and I see that shutil is used to move…
wra
  • 237
  • 4
  • 7
  • 18
5
votes
1 answer

shutil.make_archive issue - don't want directories included in zip file

I am trying to archive 2 files but not the full path leading up to the two files. Hopefully someone can point me in the right direction: My directory structure is as follows. I only have two files which I want backed up. /tmp my_stuff …
webghost
  • 75
  • 1
  • 5
5
votes
3 answers

cp -r from_dir/* to_dir with python

Is there an easy way to emulate the command cp -r from_dir/* to_dir with python? shutil.copytree is not suitable because to_dir exists.
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
5
votes
1 answer

Removing sub-folders in Python

I have an main folder(map) under this main have sub folder (zoom1,zoom2,zoom3...) how can i remove sub folder using shutil. note * : I know the main folder path sub folders are dynamically created
arun kumar
  • 105
  • 2
  • 10
5
votes
2 answers

shutil.rmtree doesn't work with Windows Library

So I'm building a simple script that backs up certain documents to my second hard-drive (you never know what could happen!). So, I used the shutil.copytree function to replicate my data on the second drive. It works beautifully, and that is not the…
user569322
5
votes
2 answers

Python: Using shutil.move or os.rename to move folders

I have written a script to move video files from one directory to another, it will also search sub directories using os.walk. however if the script finds a video file it will only move the file and not the containing folder. i have added an if…
user3560858
  • 55
  • 1
  • 7
5
votes
1 answer

Overwriting a file that already exists in the directory during shutil.move operation

I have a Source File which I'm moving to an Archive directory using shutil.move(srcfile, dstdir) But when the same file already exists in the archive destination directory it throws an error saying can't move the file exists already. So I would…
user1345260
  • 2,151
  • 12
  • 33
  • 42
4
votes
1 answer

How to copy both folder and files in python

I am aware that using shutil.copy(src,dst) copies files and shutil.copytree(src,dst) is used for copying directories. Is there any way so I don't have to differentiate between copying folders and copying files? Tysm
Luca.D
  • 57
  • 5
4
votes
3 answers

Rearranging nested directory

I have a folder system with the structure below: folderA - folder1 - file1A.txt - folder2 - file2A.txt - folder3 - file3A.txt folderB - folder1 - file1B.txt - folder2 - file2B.txt - folder3 - file3B.txt I wish to change the order…
Allentro
  • 406
  • 2
  • 13
4
votes
1 answer

How to prevent shutil.move from overwriting a file if it already exists?

I'm using this Python code in Windows: shutil.move(documents_dir + "\\" + file_name, documents_dir + "\\backup\\" + subdir_name + "\\" + file_name) When this code is called more times, it overwrites the destination file. I would like to…
xralf
  • 3,312
  • 45
  • 129
  • 200
4
votes
0 answers

Issues with shutil copy2 function

I have been using the copy2 function to copy files and it has been working fine on the Linux system with both the source and destination directories in Linux. Recently I tried to use the Windows File share as the backend and was trying to copy files…
sohank17
  • 41
  • 3
4
votes
2 answers

How to zip files (on Azure Blob Storage) with shutil in Databricks

My trained deep learning model exists out of a couple of files in a folder. So this has nothing to do with zipping dataframes. I want to zip this folder (in Azure Blob storage). But when I do with shutil this does not seem to work: import…
Axxeption
  • 273
  • 1
  • 4
  • 18