Python Forum

Full Version: Shutil attempts to copy directories that don't exist
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm attempting to copy the Documents folder under the Windows userprofile, the shutil.copytree(documents, destination) works partially. It will copy the Documents to the root of the destination, in this case the R: drive, and it will also attempt to copy the other Windows special folders (My Music, My Pictures, etc..), even if they don't exist under Documents.

import shutil


def main():

   try:

       user_profile = os.getenv("USERPROFILE")

       # Construct a full path to the documents folder 
       documents = Path(user_profile).joinpath("Documents")

       # Destination for the Documents folder
       destination = Path("R:\\Test")

       shutil.copytree(documents, destination)            

   except (FileNotFoundError, shutil.Error) as error:
       print(error)


if __name__ == '__main__':
    main()
This is a excerpt of the exception that is thrown:
[('C:\\Users\\ConsoleGeek\\Documents\\My Music', 'R:\\Test\\My Music', 
"[WinError 5] Access is denied: 'C:\\\\Users\\\\ConsoleGeek\\\\Documents\\\\My Music'")
...
None of these folders exist under Documents, so I don't fully understand why shutil attempts to copy these special folders. If I attempt to copy a regular folder under the user profile, it works.

I also posted this question on Stack Overflow, but no one has answered.
I don't know the Windows system well enough but you could try calling
shutil.copytree(documents, destination, symlinks=True)
if these 'special folders' are symbolic links (or the Windows equivalent of symbolic links).
ConsoleGeek Wrote:I don't fully understand why shutil attempts to copy these special folders
What is the output of
print(os.listdir(documents))
Does it show these folders?
(Oct-29-2019, 02:59 PM)Gribouillis Wrote: [ -> ]print(os.listdir(documents))

Directly from the IDE:

['desktop.ini', 'My Music', 'My Pictures', 'My Videos']
Strange thing to happen, because Documents is empty, even when I enable hidden folders, it's empty. copytree only successfully copies the .ini file. I guess the solution is to use ignore_patterns, check to see if the Documents folder is the item we're working with, and ignore those folders.
(Oct-29-2019, 02:59 PM)Gribouillis Wrote: [ -> ]
ConsoleGeek Wrote:I don't fully understand why shutil attempts to copy these special folders
What is the output of
print(os.listdir(documents))
Does it show these folders?

I posted a detailed answer on Stack Overflow.
The question seems to be unrelated to Python. It is rather a question about the Windows OS or even a question about the configuration of FileExplorer in Windows: why doesn't it show these folders, which are present?