Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd tackle this problem this way: There are 3 alternatives for copying a file:</p> <ol> <li>On Unix, you can use <a href="http://docs.python.org/library/os.html#os.link" rel="nofollow"><code>os.link</code></a>.</li> <li><p>On Windows, you can use a function like this:</p> <pre><code>def CreateHardLink(src, dst): import ctypes if not ctypes.windll.kernel32.CreateHardLinkA(dst, src, 0): raise OSError </code></pre> <p>(Disclaimer: I haven't tested it. The idea is to use the kernel32 API.)</p></li> <li>If all this fails, you can still make a regular file copy (for example, with <code>shutil.copy2</code>).</li> </ol> <p>Then, you can test these 3 functions at the beginning of the script with a temporary file (to be deleted afterwards). The first of these 3 functions that succeeds (that is, the first one that raises no <code>Exception</code>) is a good candidate for making the actual backup copies.</p> <p>This idea follows a <a href="http://en.wikipedia.org/wiki/Strategy_pattern" rel="nofollow">strategy pattern</a>, where you have to select a function that takes the source and destination filenames and makes the best effort to make the copy the way you want it.</p> <p>Let me add one more thing: Detecting the filesystem in order to choose a copying strategy is not a very good practice in the same way that detecting browsers is not a good practice to do different things in Javascript. It's always best to try to use a feature and be prepared to catch exceptions or handle errors in order to produce an alternative result.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload