0

I have an annual file located on our netwrok that contains sub-folders based on each week of the year. I am trying to write a script that will copy a "Template" document saved on my desktop, then iterate through the annual file, and paste the Template into each weekly sub-folder.

I have a script that is generating a "PermissionError: [Errno 13] Permission denied:" though I am not sure why, and therefore not sure what to do about it.

import os
import shutil

myPath = r"""K:\PROCESS\YEAR"""
myDocument = r"""C:\Users\me.domain\OneDrive - Co Name\Desktop\Template.xlsx"""

for filename in os.listdir(myPath):
    with open(os.path.join(myPath,filename)) as myFile:
        copyfile(myDocument, myFile)

Error is,

PermissionError: [Errno 13] Permission denied: ‘K:\PROCESS\YEAR\WEEK 1’

@ line 'with open(os.path.join(myPath,filename)) as myFile:'

I tried moving the target document to the program root and modified the script based on some sugestions as seen below.

import os
from shutil import copy2

my_doc_path = "Template.xlsx"
myPath = "K:\PROCESS\YEAR"

for directory in os.listdir(myPath):
    copy2(my_doc_path, directory)

I am no longer receiving any errors; however, the target document is not being coppied to the target directory. Instead, a nondescript "file" with the target directory name is being created in the program root as seen in the image below.

enter image description here


What ended up working:

import os
from shutil import copy

my_doc_path = "Template.xlsx"
myPath = "K:\PROCESS\YEAR"

for directory in os.listdir(myPath):
    
    target_directory = myPath + '\\' + directory
    check_file = target_directory + '\\' + my_doc_path
    
    if not os.path.exists(check_file):
        copy(my_doc_path, target_directory)
Dru
  • 73
  • 9

2 Answers2

1

Instead of opening a file, try to copy your file in the target folders.

from shutil import copy

my_doc_path = "/here/is/my/doc/path.xlsx"
myPath = "K:\PROCESS\YEAR"

for directory in os.listdir(myPath):
    copy(my_doc_path, directory)

Or if you need also the metadata:

from shutil import copy2

my_doc_path = "/here/is/my/doc/path.xlsx"
myPath = "K:\PROCESS\YEAR"

for directory in os.listdir(myPath):
    copy2(my_doc_path, directory)

Why copy/copy2 instead of copyfile? take a look at this comment

Mattia
  • 961
  • 13
  • 25
  • 1
    I think you should rather use copy2, as it copies metadata too. – TheEagle Feb 01 '21 at 17:27
  • 1
    (Unless of course the OP doesn't want that, but he didn't mention that) – TheEagle Feb 01 '21 at 17:27
  • 1
    Yes, of course, it depends of what is the OP needs :) (add also the copy2 example) – Mattia Feb 01 '21 at 17:29
  • So, it dosnt look like this is copying the targt file "my_doc_path" to the the target folder, it looks like it is creating a "file" with the target folder name in the application root. The file has no file type, jsut the name of the target folder – Dru Feb 01 '21 at 17:47
0

The document name needed to be appended to myPath variable. I incorrectly assumed the copy2() function was looking for the path to save the document to, I didnt realize it required the full destination path including the document itself.

Thanks for the feedback everyone, it got me pointed in the right direction.

Dru
  • 73
  • 9