MCPcopy Index your code
hub / github.com/python/cpython / copyfile

Function copyfile

Lib/shutil.py:283–353  ·  view source on GitHub ↗

Copy data from src to dst in the most efficient way possible. If follow_symlinks is not set and src is a symbolic link, a new symlink will be created instead of copying the file it points to.

(src, dst, *, follow_symlinks=True)

Source from the content-addressed store, hash-verified

281 return fn.is_symlink() if isinstance(fn, os.DirEntry) else os.path.islink(fn)
282
283def copyfile(src, dst, *, follow_symlinks=True):
284 """Copy data from src to dst in the most efficient way possible.
285
286 If follow_symlinks is not set and src is a symbolic link, a new
287 symlink will be created instead of copying the file it points to.
288
289 """
290 sys.audit("shutil.copyfile", src, dst)
291
292 if _samefile(src, dst):
293 raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
294
295 file_size = 0
296 for i, fn in enumerate([src, dst]):
297 try:
298 st = _stat(fn)
299 except OSError:
300 # File most likely does not exist
301 pass
302 else:
303 # XXX What about other special files? (sockets, devices...)
304 if stat.S_ISFIFO(st.st_mode):
305 fn = fn.path if isinstance(fn, os.DirEntry) else fn
306 raise SpecialFileError("`%s` is a named pipe" % fn)
307 if _WINDOWS and i == 0:
308 file_size = st.st_size
309
310 if not follow_symlinks and _islink(src):
311 os.symlink(os.readlink(src), dst)
312 else:
313 with open(src, 'rb') as fsrc:
314 try:
315 with open(dst, 'wb') as fdst:
316 # macOS
317 if _HAS_FCOPYFILE:
318 try:
319 _fastcopy_fcopyfile(fsrc, fdst, posix._COPYFILE_DATA)
320 return dst
321 except _GiveupOnFastCopy:
322 pass
323 # Linux / Android / Solaris
324 elif _USE_CP_SENDFILE or _USE_CP_COPY_FILE_RANGE:
325 # reflink may be implicit in copy_file_range.
326 if _USE_CP_COPY_FILE_RANGE:
327 try:
328 _fastcopy_copy_file_range(fsrc, fdst)
329 return dst
330 except _GiveupOnFastCopy:
331 pass
332 if _USE_CP_SENDFILE:
333 try:
334 _fastcopy_sendfile(fsrc, fdst)
335 return dst
336 except _GiveupOnFastCopy:
337 pass
338 # Windows, see:
339 # https://github.com/python/cpython/pull/7160#discussion_r195405230
340 elif _WINDOWS and file_size > 0:

Callers 2

copyFunction · 0.85
copy2Function · 0.85

Calls 15

_samefileFunction · 0.85
SameFileErrorClass · 0.85
enumerateFunction · 0.85
_statFunction · 0.85
_islinkFunction · 0.85
_fastcopy_fcopyfileFunction · 0.85
_fastcopy_sendfileFunction · 0.85
_copyfileobj_readintoFunction · 0.85
SpecialFileErrorClass · 0.70
openFunction · 0.70
copyfileobjFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…