0
#!/usr/bin/env python

from os import path, access, R_OK  # W_OK for write permission.
import os`enter code here`
import shutil
import sys
import glob

PATH = 'C:\Windows\PsExec.exe'
PATH2 = 'C:\Windows'
SHARE_PATH = '\\\\blue\\install$\\Tools\\Library'
dirList=os.listdir(SHARE_PATH)

if path.exists(PATH) and path.isfile(PATH) and access(PATH, R_OK):
    print ("File exists and is readable")
elif path.exists(SHARE_PATH) and access(SHARE_PATH, R_OK):
    shutil.copyfile(SHARE_PATH, PATH2)
    print ("Copying File")

I can run this Script without a Error but for some reason I cannot copy the file from
the share drive... and now when I tried to run the file I got the following error.

Traceback (most recent call last):
  File ".\file_reader3.py", line 18, in <module>
    shutil.copyfile(SHARE_PATH, PATH2)
  File "C:\Python33\lib\shutil.py", line 109, in copyfile
    with open(src, 'rb') as fsrc:
PermissionError: [Errno 13] Permission denied: '\\\\blue\\install$\\Tools\\Library'
2
  • Is '\\\\blue\\install$\\Tools\\Library' a folder or a file? copyfile only works on files I believe Commented Mar 29, 2013 at 16:00
  • I want to copy a single file from the share drive to my C:\Windows Commented Mar 29, 2013 at 17:05

2 Answers 2

2

It looks like the line "shutil.copyfile(SHARE_PATH, PATH2)" is trying to copy one directory '\\blue\install$\Tools\Library' to another directory 'C:\Windows'. copyfile is just for, well, files.

Also per the docs (http://docs.python.org/2/library/shutil.html) you need to specify the full file path AND name. So assuming '\\blue\install$\Tools\Library' is a file (although I think it's a directory), it's trying to copy it to a file called "c:\windows" not copying into that directory. So you need to specify "c:\windows\filename" as the second parameter.

If you're trying to copy an entire directory, try shutil.copytree.

Also, it's probably better to just try to open the file and catch the exception rather than testing for permissions first. see http://docs.python.org/2/library/os.html#files-and-directories

Sign up to request clarification or add additional context in comments.

1 Comment

from os import path, access, R_OK # W_OK for write permission. import os import shutil import sys import glob PATH = 'C:\Windows\PsExec.exe' SHARE_PATH = '\\\\tumbler\\install$\\Tools\\Library\\PsExec.exe' #This part Will check if the file Exist if path.exists(PATH) and path.isfile(PATH) and access(PATH, R_OK): print ("File exists and is readable") elif path.exists(SHARE_PATH) and \ path.isfile(SHARE_PATH) and \ access(SHARE_PATH, R_OK): shutil.copy(SHARE_PATH, PATH) print ("Copying File")
2
from os import path, access, R_OK  # W_OK for write permission.
import os
import shutil
import sys
import glob


PATH = 'C:\Windows\PsExec.exe'
SHARE_PATH = '\\\\blue\\sol\\Tools\\Library\\PsExec.exe'

#This part Will check if the file Exist
if path.exists(PATH) and path.isfile(PATH) and access(PATH, R_OK):
    print ("File exists and is readable")
#This part will Check if the file exist on the server, and copy to the local machine
elif path.exists(SHARE_PATH) and \
     path.isfile(SHARE_PATH) and \
     access(SHARE_PATH, R_OK):
    shutil.copy(SHARE_PATH, PATH)
    print ("Copying File")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.