`shutil.copyfile` Error: Permissions Denied

2019-08-17 14:37发布

问题:

I have worked through a number of other threads on this, but not of their solutions seem to work here, that or I am not understanding properly, and would love your help.

I am getting a:

IOError: [Errno 13] Permission denied: 'W:\\test\\Temporary Folder 195\\Sub-fold1 

This is the general code i started with.

    summary_file = r'W:/test/SDC Analysis Summary.docm'
    shutil.copyfile(summary_file, os.getcwd())

I have also varied this a little bit based on other threads, specifically replacing summary_file with the actual text and also adding \ to the end of working directory without success. Really don't know what I'm missing here. I know that the Documentation is looking for complete paths, but I believe I am satisfying that requirement. What am I missing here?

Note: there is a desire to use copyfile over copy due to the speed increase.

回答1:

From the documentation:

 dst must be the complete target file name

You can't just use os.getcwd() as the destination.



回答2:

you should be the complete target file name for destination

destination = pathdirectory + filename.*

I use this code fir copy wav file with shutil :

    # open file with QFileDialog

    browse_file = QFileDialog.getOpenFileName(None, 'Open file', 'c:', "wav files (*.wav)")

    # get file name 

    base = os.path.basename(browse_file[0])
    os.path.splitext(base)
    print(os.path.splitext(base)[1])

    # make destination path with file name

    destination= "test/" + os.path.splitext(base)[0] + os.path.splitext(base)[1]
    shutil.copyfile(browse_file[0], destination)