Skip to content Skip to sidebar Skip to footer

How To Clone Files With Python?

Using bash on macos I can create COW file clones with cp -c. Is there a Python library that provides the same functionality? The copy functions in shutil doesn't seem to mention cl

Solution 1:

The Python standard library does not support cloning files.

To clone a file using cp -c in a subprocess you can use this function:

def clonefile(source, dest):
    subprocess.check_output(["cp", "-c", source, dest])

Solution 2:

Ok, I marked this as a duplicate but didn't know the difference between copying and cloning.

This is the location of the instructions for copying files with as much detail as possible in Python: https://docs.python.org/3.6/library/shutil.html

It starts with the following warning: Warning Even the higher-level file copying functions (shutil.copy(), shutil.copy2()) cannot copy all file metadata.

If what you want is an OS cloning of the file, you're going to need to run a system call. That system call is going to need to be OS specific, and the call will be pulled from this question: Calling an external command in Python

So, look into the copy library and see if you can get enough detail, and if you can't use a system-specific call.

Post a Comment for "How To Clone Files With Python?"