Python Forum
Shutil attempts to copy directories that don't exist
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shutil attempts to copy directories that don't exist
#1
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.
Reply
#2
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).
Reply
#3
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?
Reply
#4
(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.
Reply
#5
(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.
Reply
#6
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 250 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Organization of project directories wotoko 3 423 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  use of shutil.copytree with ENOTDIR exception yan 2 899 Nov-29-2023, 03:02 PM
Last Post: yan
  Failed attempts to load Microsoft Appstore Python DLLs piyushd 0 424 Oct-31-2023, 10:43 AM
Last Post: piyushd
  UndefinedEnvironmentName: 'extra' does not exist in evaluation environment EarthAndMoon 3 1,680 Oct-09-2023, 05:38 PM
Last Post: snippsat
  Listing directories (as a text file) kiwi99 1 834 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  shutil.move make data corrupt kucingkembar 0 788 Feb-01-2023, 01:30 PM
Last Post: kucingkembar
  Find duplicate files in multiple directories Pavel_47 9 3,073 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  rename same file names in different directories elnk 0 711 Nov-04-2022, 05:23 PM
Last Post: elnk
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,584 Aug-28-2022, 07:11 AM
Last Post: Melcu54

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020