Python Forum
use of shutil.copytree with ENOTDIR exception
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
use of shutil.copytree with ENOTDIR exception
#1
Hello,

First thank you to help people !

I would like to make a script to copy an entire tree for testing (folder, sub-folders, files).
I need all but the files are too big in the source and I don't need what's in them.
So I would like to copy the names of the files to recreate them empty in the destination folders.

With copytree, the copy works.
I was thinking of using the ENOTDIR exception to prevent files from being copied, retrieve their name and then create the empty equivalent file in the destination.
But for the moment, it does not take into account my exception and the copy is just done in full without error.
Could you help me please ?
Thank you.

# Source
src = '/mysourcepath'
 
# Destination
dest = '/mydestinationpath'
 
# Copy the content of src to dst
try:
    shutil.copytree(src, dest, dirs_exist_ok=True)
    
except OSError as err:
     # error caused if the source is not a directory
    if err.errno == errno.ENOTDIR:
        # create a new file with the good name in the destination folder
        f = open(dest, "x")
    else:
        print("Error: % s" % err)
Reply
#2
(Nov-29-2023, 09:48 AM)yan Wrote: I would like to make a script to copy an entire tree for testing (folder, sub-folders, files).
I need all but the files are too big in the source and I don't need what's in them.
This is somewhat contradictory, but you could perhaps try the following
from pathlib import Path

def my_copy(src, dst):
    # create an empty file instead of actually copying a file
    Path(dst).touch()

# Source
src = '/mysourcepath'
  
# Destination
dest = '/mydestinationpath'
  
shutil.copytree(src, dest, dirs_exist_ok=True, copy_function=my_copy)
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Ok, thank you for your help ! Thumbs Up
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  shutil.move make data corrupt kucingkembar 0 801 Feb-01-2023, 01:30 PM
Last Post: kucingkembar
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,606 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  extra slashes in the network path for shutil.copy tester_V 3 3,784 Jun-02-2021, 07:57 AM
Last Post: supuflounder
  concatenat - shutil jmabrito 3 2,207 Feb-11-2021, 12:17 PM
Last Post: jmabrito
  Shutil FileNotFoundError: Errno 2 Help lord_kaiser 8 10,556 Aug-10-2020, 08:45 AM
Last Post: lord_kaiser
  shutil.copy questions kristianpython 3 2,328 Jul-14-2020, 09:19 AM
Last Post: Gribouillis
  Shutil move if file exists in destination Friend 2 6,782 Feb-02-2020, 01:45 PM
Last Post: Friend
  Shutil attempts to copy directories that don't exist ConsoleGeek 5 4,566 Oct-29-2019, 09:26 PM
Last Post: Gribouillis
  Sutil.copytree, I'm getting File Exists. Mike Ru 1 8,642 Oct-28-2019, 07:40 AM
Last Post: Gribouillis
  Help copying files with shutil hikerguy62 2 4,314 Aug-02-2019, 08:16 PM
Last Post: hikerguy62

Forum Jump:

User Panel Messages

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