120 of 151 menu

The copy2 method of shutil module

The copy2 method of the shutil module copies a file while preserving its metadata. The first parameter of the method specifies the path to the source file, the second parameter specifies the destination path of the new file. The third optional parameter specifies work with symbolic links (by default, follow_symlinks=True).

Syntax

import shutil shutil.copy2(where do we copy from, where do we copy to, [mode for symbolic links])

Example

Let's copy the file file1.txt to the file file2.txt:

import shutil shutil.copy2('file1.txt', 'file2.txt')

The result of the executed code:

file2.txt

See also

  • method copy of module shutil,
    which copies a file while preserving the access mode
  • method copytree of module shutil,
    which recursively copies a folder
  • method move of module shutil,
    which recursively moves a file or directory
  • method path.isfile of module os,
    which checks the existence of a file
  • method makedirs of module os,
    which creates a directory
  • method mkdir of module os,
    which creates one folder
  • method remove of module os,
    which deletes the file
  • method getcwd module os,
    which returns the current working directory
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline