In [1]:
import shutil  #importing show utilities module
In [2]:
shutil.copy('F:\\Example\\something.txt', 'F:\\Example2')
Out[2]:
'F:\\Example2\\something.txt'

There is a copy of something.txt in the Example2 folder.

Rename and copy at the same time can be done by specifying the file name for the destination.

In [3]:
shutil.copy('F:\\Example\\something.txt', 'F:\\Example2\\newName.txt')
Out[3]:
'F:\\Example2\\newName.txt'
In [4]:
import os
os.listdir('F:\\Example2')
Out[4]:
['newName.txt', 'something.txt']

To copy entire folders:

In [5]:
shutil.copytree('F:\\Example','F:\\Example2\\Example_backup')
Out[5]:
'F:\\Example2\\Example_backup'
In [6]:
os.listdir('F:\\Example2')
Out[6]:
['Example_backup', 'newName.txt', 'something.txt']

copied the folder 'Example' to the Example2 folder with name 'Example_backup'.


In [7]:
shutil.move('F:\\Example\\something.txt','F:\\Example2\\something2.txt')
Out[7]:
'F:\\Example2\\something2.txt'
In [8]:
os.listdir('F:\\Example2')
Out[8]:
['Example_backup', 'newName.txt', 'something.txt', 'something2.txt']

There is no rename function in shutil, instead we move the file to the same destination with new name.

In [9]:
shutil.move('F:\\Example2\\Example_backup','F:\\Example2\\backup_Example')
Out[9]:
'F:\\Example2\\backup_Example'
In [10]:
os.listdir('F:\\Example2')
Out[10]:
['backup_Example', 'newName.txt', 'something.txt', 'something2.txt']



-shutil.copy() will copy a file

-shutil.copytree() will copy a folder with it's contents

-shutil.move() can move a file but also rename it aswell