'Python PermissionError: [WinError 5] Access is denied Pathlib
I am attempting to move a files from one directory to another utilizing the pathlib library, however, I am receiving a permission error in python from the following code:
import pathlib
#create a folder called documents with the files
my_dir = pathlib.Path.cwd()
documents = my_dir / "documents"
if not documents.exists():
documents.mkdir()
files = [documents / "image1.png", documents / "image2.gif", documents / "image3.png", documents / "image4.jpg"]
for file in files:
if not file.exists():
file.touch()
#create a folder called images and move the files from docs into images
images = my_dir / "images"
if not images.exists():
images.mkdir()
for path in documents.rglob("*.*"):
path.replace(images / path.name)
print(images.iterdir())
Anyone know where I am getting it wrong? Tried running the program in Administrator mode, no luck.
The Error is as follows:
Traceback (most recent call last):
File "C:\Users\Computer\Desktop\playingwfiles.py", line 25, in <module>
path.replace(images / path.name)
File "C:\Users\Computer\AppData\Local\Programs\Python\Python39 \lib\pathlib.py", line 1395, in replace
self._accessor.replace(self, target)
PermissionError: [WinError 5] Access is denied: 'C:\\Users\\Computer\\Desktop\\documents\\image1.png' -> 'C:\\Users\\Computer\\Desktop\\images\\image1.png'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
