125 of 151 menu

The move method of the shutil module

The move method of the shutil module moves a file or folder. Folders are moved along with their contents.

The first parameter of the method specifies the path to the source file, the second parameter specifies the destination path of the new file, and the third optional parameter specifies the file copy function (by default copy_function=copy2).

The method returns the destination path of the new file.

If the original and new paths are the same, the method simply overwrites the contents of the specified file.

Syntax

import shutil shutil.move(where do we copy from, where do we copy to, [copy function])

Example

Let's move the file file1.txt:

import shutil print(shutil.move('file1.txt', 'file2.txt'))

Example

Let's move the folder dir with all its contents:

import shutil print(shutil.move('dir', 'trg'))

See also

  • method remove of module os,
    which deletes the file
  • method copy of module shutil,
    which copies a file while preserving the access mode
  • method getcwd module os,
    which returns the current working directory
  • method path.exists of module os,
    which checks the existence of a path
  • method path.isdir of module os,
    which checks the existence of a folder
  • method path.isfile of module os,
    which checks the existence of a file
  • method scandir of module os,
    which iterates through the files in a folder
  • method rmtree of module shutil,
    which recursively deletes a folder
  • method copy2 of module shutil,
    which copies a file with metadata
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