PermissionError: [Errno 13] Permission denied. What to do?

0 like 0 dislike
import zipfile, os import shutil a = os.listdir(path="D:\\домашка\\G_ Arhangelskiy_-_Time_Draive\\G_ Arhangelskiy_-_Time_Draive") for i in a: s='D:\\домашка\\G_ Arhangelskiy_-_Time_Draive\\G_ Arhangelskiy_-_Time_Draive\\\\'+str(i) b = os.listdir(path=s) for j in b: s1 = 'D:\\домашка\\G_ Arhangelskiy_-_Time_Draive\\G_ Arhangelskiy_-_Time_Draive\\\\'+i+'\\\\'+j shutil.copyfile(s1, "D:\\домашка\\G_ Arhangelskiy_-_Time_Draive\\G_ Arhangelskiy_-_Time_Draive — copy") os.remove('D:\\домашка\\G_ Arhangelskiy_-_Time_Draive\\G_ Arhangelskiy_-_Time_Draive\\\\'+i)

Traceback (most recent call last): File "C:\\Users\\Alex\\PycharmProjects\\untitled\\из paparo", line 9, in shutil.copyfile(s1, "D:\\домашка\\G_ Arhangelskiy_-_Time_Draive\\G_ Arhangelskiy_-_Time_Draive — copy") File "C:\\Users\\Alex\\AppData\\Local\\Programs\\Python\\Python36\\lib\\", line 115, in copyfile with open(dst, 'wb') as fdst: PermissionError: [Errno 13] Permission denied: 'D:\\\\домашка\\\\G_ Arhangelskiy_-_Time_Draive\\\\G_ Arhangelskiy_-_Time_Draive — copy'

What to do?
by | 5 views

2 Answers

0 like 0 dislike
Didn't work likely due to the fact that "\\" is special. symbol. Used to refer to such things as end-of-line (\), tab (\\t). In Your case, most likely trying to understand what it means to \\d, \\G, etc. To write the \\ character in strings, use \\\\.
Depending on the version of Python may be a problem in the encoding path. Because there is a difference of Russian characters in cp1251 and UTF8 which is usually used in Windows.

I would write this:
import os from shutil import copyfile, rmtree SOURCE = 'D:\\\\домашка\\\\G_ Arhangelskiy_-_Time_Draive\\\\G_ Arhangelskiy_-_Time_Draive' DESTINATION = 'D:\\\\домашка\\\\G_ Arhangelskiy_-_Time_Draive\\\\G_ Arhangelskiy_-_Time_Draive — copy' files = [f.path for f in os.scandir(SOURCE) if f.is_file()] for file in files: copyfile(file, DESTINATION) rmtree(SOURCE)

How experienced Python developers can give some advice:
  • Duplicate values in a separate variable (it would be better in a constant, but Python does not have constants). It's about the way the source that I have identified in the SOURCE (there is such an agreement, immutable variables when writing the title)
  • To delete a path recursively, is in shutils module rmtree and os.scandir more convenient than os.listdir
  • Install Linter (flake8), and then configure your editor to use it. Code shows that don't use the Linter
0 like 0 dislike
Firstly to repeat the experiment making sure that there are no Russian characters and spaces in paths
Secondly, to check whether the rights to read/write a normal user
110,608 questions
257,186 answers
28,699 users