Python - Errno 13 Permission denied when trying to copy files
<p>I am attempting to make a program in Python that copies the files on my flash drive (letter D:) to a folder on my hard drive but am getting a <em>PermissionError: [Errno 13] Permission denied: 'D:'</em>.</p>

<p>The problematic part of my code is as follows:</p>

<pre><code># Copy files to folder in current directory
def copy():
    source = getsource()

    if source != "failure":

        copyfile(source, createfolder())
        wait("Successfully backup up drive"
             "\nPress 'Enter' to exit the program")

    else:
        wait("No USB drive was detected"
             "\nPress 'Enter' to exit")

# Create a folder in current directory w/ date and time
def createfolder():
    name = strftime("%a, %b %d, %Y, %H.%M.%S", gmtime())
    dir_path = os.path.dirname(os.path.realpath(__file__))
    new_folder = dir_path + "\\" + name
    os.makedirs(new_folder)

return new_folder
</code></pre>

<p>Everything seems to run fine until the <em>copyfile()</em> function runs, where it returns the error.
I tried replacing <em>getsource()</em> with the destination of the file instead and it returned the same permission error except for the <em>new_folder</em> directory instead.</p>

<p>I have read several other posts but none of them seem to be relevant to my case. I have full admin permissions to both locations as well.
Any help would be greatly appreciated!</p>

<p>As I stated in my comment above, it seems as if you're trying to open the directory, <code>D:</code>, as if it was a file, and that's not going to work because it's not a file, it's a directory.</p>

<p>What you can do is use <code>os.listdir()</code> to list all of the files within your desired directory, and then use <code>shutil.copy()</code> to copy the files as you please.</p>

<p>Here is the documentation for each of those:</p>

<p><a href="https://docs.python.org/3/library/os.html#os.listdir" rel="nofollow">os.listdir()</a> (You will be passing the full file path to this function)</p>

<p><a href="https://docs.python.org/3/library/shutil.html#shutil.copy" rel="nofollow">shutil.copy()</a> (You will be passing each file to this function)</p>

<p>Essentially you would store all of the files in the directory in a variable, such as <code>all_the_files = os.listdir(/path/to/file)</code>, then loop through <code>all_the_files</code> by doing something like <code>for each_file in all_the_files:</code> and then use <code>shutil.copy()</code> to copy them as you please.</p>

python copyfile