A
A
Anisifor2021-07-05 20:57:50
Python
Anisifor, 2021-07-05 20:57:50

Can't copy file via shutil. [Errno 13] Permission denied. What to do?

You need to use python to copy the file (folder with files) from the directory C:\Users\User\Documents to the directory C:\Users\User\AppData\files\results\Crossout . The question is how to do it with python if shutil writes: PermissionError: [Errno 13] Permission denied: 'C:\\\\Users\\\\User\\\\Documents\\\\My Games\\\\ Crossout? Maybe you can use some other libraries, or am I mistaken somewhere?
The code:

import getpass
import os
import shutil
user = getpass.getuser()
Path = rf"C:\\Users\\{user}\\AppData\\files\\results"
Crossout = rf'C:\\Users\\{user}\\Documents\\My Games\\Crossout'
if (os.path.exists(Crossout)) == True:
    if not os.path.exists(Path + r"\\Crossout"):
         os.mkdir(Path + r"\\Crossout")
    shutil.copyfile(Crossout, Path + r"\\Crossout\\Crossout Passwords", follow_symlinks=True)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
12rbah, 2021-07-06
@12rbah

PermissionError: [Errno 13] Permission denied: there seems to be a permissions issue here
os.mkdir(path, mode=0o777)

V
Vindicar, 2021-07-06
@Vindicar

First, get familiar with the pathlib standard library for working with paths, it will be much easier. In particular, look towards Path.expanduser() .
Second, understand the exist_ok parameter in the directory creation and copy functions so you don't have to write if's to create directories.
Third, copyfile() only works on files, as the name suggests. She does not understand what to do with the directory. The behavior you expect is provided by the shutil.copytree() function .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question