Python copy file (Examples)

In this python tutorial, we will discuss the Python copy file. We will also check:

  • Python copy file to a folder
  • Python copy files from one location to another
  • Python copy file from one directory to another
  • Python copy file to USB
  • Python copy file content to another file
  • Python copy file and rename
  • Python copy file from subfolders to one folder
  • Python copy file to the current directory
  • Python copy file line by line
  • Python copy file overwrite

Let us check on how to copy files in Python with a few examples.

Python copy file
Python copy file

Python shutil.copy()method

The shutil.copy() method in Python is used to copy the files or directories from the source to the destination. The source must represent the file, and the destination may be a file or directory.

This function provides collection and operations on the files it also helps in the copying and removal of files and directories.

Python copy file to folder

Now, we can see how to copy file in python.

  • In this example, I have imported two modules called shutil, and os this module helps in automating the process of copying and removing files and directories.
  • We have to write a path for both source and destination. We have to use shutil.copyfile(src, dst) to copy the file from source to destination. Along with the path, the name of the file with its extension should be written.
  • src = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\name.txt’ is the source path. name.txt is the filename that I have created with the .txt extension.
  • dst = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\name.txt’ is the destination path. Here, I have created a folder called Newfolder to copy the file name.txt in the Newfolder.

Example:

import shutil
import os
src = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\name.txt'
dst =  r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\name.txt'
shutil.copyfile(src, dst)

Python copy files from one location to another

Here, we can see how to copy files from one location to another in Python

  • In this example, I have imported two modules called shutil and os. Here, the shutil module helps in automating the process of coping and removing files and directories.
  • src= r’C:\Newfile\file2.txt’ is the source path. file2.txt is the filename that I have created with the .txt extension.
  • dst = r’D:\file2.txt’ destination path. file2.txt is the filename that I have created with the .txt extension.
  • Here, I have copied the file from C drive to D drive
  • To copy the file from one location to another, we have to use shutil.copyfile(src, dst).

Example:

import shutil
import os
src= r'C:\Newfile\file2.txt'
dst = r'D:\file2.txt'
shutil.copyfile(src, dst)

Python copy file from one directory to another

Here, we can see how to copy file from one directory to another in Python.

  • In this example, I have imported modules called shutil and os in Python. The shutil module helps in automating the process of coping and removing files and directories.
  • src = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\file2.txt’ is the source path. file2.txt is the name of the file with the .txt extension.
  • dst = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\file2.txt’ is the destination path. file2.txt is the name of the file with the .txt extension.
  • Work and New folder are the two directories.
  • To copy the file from one directory to another, we have to use shutil.copyfile(src, dst).

Example:

import shutil
import os
src = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\file2.txt'
dst = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\file2.txt'
shutil.copyfile(src, dst)

Example:

Python copy file to USB

Here, we can see how to copy a file to USB in Python.

  • In this example, I have imported modules called shutil and os. The shutil module helps in automating the process of coping and removing files and directories.
  • I have used shutil.copy(src, dst) to copy the file to USB.
  • src=r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\name.txt is the path of the file, name.txt is the filename that I have created with the .txt extension.
  • dst=r’D:\lakshmi\name.txt’ is the path of the USB. The name.txt is the filename that I have created with the .txt extension. In this “lakshmi” is the folder name and the filename is name.txt which is copied to “r’D:\lakshmi\name.txt’ “ (which is our USB drive).
  • Along with the path, the name of the file and its extension should be written.

Example:

import shutil
shutil.copy(src=r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\name.txt', dst=r'D:\lakshmi\name.txt') 

Python copy file content to another file

Now, we can see how to copy file content to another file in Python.

  • In this example, I have imported two modules called os and sys. The sys module provides functions and variables used to manipulate the program.
  • f = open(r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\employee.txt’,”r”) is the source path. The employee.txt is the file name that I have created with the .txt extension.
  • copy = open(r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\employee.txt’,”wt”) is destination path. The employee.txt is the file name that I have created with the .txt extension.
  • line = f.read() reads the specified bytes from the file to another file.
  • copy.write(str(line)) is used to write each line in the file.
  • f.close is used to close the opened file. It is necessary to use a copy.close() while iterating a file in python.

Example:

import os
import sys
f = open(r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\employee.txt',"r")
copy = open(r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\employee.txt',"wt")
line = f.read()
copy.write(str(line))
f.close()
copy.close()

Python copy file and rename

Here, we can see how to copy file and rename in Python.

  • In this example, I have imported Python modules called shutil and os.
  • Firstly, we have to copy the file from source to destination and then rename the copied file.
  • src = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\name.txt’ is the source path. name.txt is the file name that I have created with the .txt extension.
  • dst = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\name.txt’ is the destination path. The name.txt is the file name that I have created with the .txt extension.
  • os.rename is used to rename the folder name. To rename the file, I have used os.rename(r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\name.txt’,r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\details.txt’)
  • shutil.copyfile(src, dst) is used to copy the file from source to destination.
  • The name.txt file name is now renamed by the name called details.txt.

Example:

import shutil
import os
src = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\name.txt'
dst = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\name.txt'

shutil.copyfile(src, dst)

os.rename(r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\name.txt',r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\details.txt' )

Python copy file from subfolders to one folder

Here, we will see how to copy file from subfolder to one folder in Python.

  • In this example, I have used two modules called shutil, and os. The shutil module helps in automating the process of coping and removing files and directories.
  • The os module includes many functionalities like interacting with the filesystem.
  • src= r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\office\employee.txt’. is the source path, employee.txt is the filename that I have created with the .txt extension.
  • dst = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\employee.txt‘. is the destination path. The employee.txt is the filename that I have created with the .txt extension.
  • The office is a subfolder and a New folder is another folder in this example.

Example:

import shutil
import os
src =  r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\office\employee.txt'
dst = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\employee.txt'
shutil.copyfile(src, dst)

Python copy file to the current directory

Now, we can see how to copy file to the current directory in Python.

  • In this example, I have used two modules called shutil, and os. The shutil module helps in automating the process of coping and removing files and directories.
  • The os module includes many functionalities like interacting with the filesystem.
  • src = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\office\employee.txt’ is the source path. employee.txt is the filename that I have created with the .txt extension.
  • dst = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\employee.txt’ is the destination path. The employee.txt is the filename that I have created with the .txt extension.
  • Work is the current directory in this example.
  • shutil.copyfile(src, dst) is used to copy the file to the current directory.

Example:

import shutil
import os
src = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\office\employee.txt'
dst = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\employee.txt'
shutil.copyfile(src, dst)

Python copy file line by line

Now, we can see how to copy file line by line in python.

  • In this example, I have imported two modules called os and sys. The sys module provides functions and variables used to manipulate the program.
  • src = open(r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\employee.txt’,”r”) is the source path. The employee.txt is the filename that I have created with the .txt extension.
  • dst = open(r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\employee.txt’,”wt”) is the destination path. The employee.txt is the filename that I have created with the .txt extension.
  • I have used the iterating function to iterate the file object in the range i = 1 and i < 3. So, only 2 lines from the file will be copied. The copy.write(str(line)) is used to write a single string into the file.
  • “r” mode is used to read the file, “wt” is used to write the file. The f.close is used to close the file.
  • f.read returns the specified number of bytes from the file.

Example:

import os
import sys
i = 1
src = open(r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\employee.txt',"r")
dst = open(r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Newfolder\employee.txt',"wt")
while i < 3:
    line = f.readline()
copy.write(str(line))
i = i +1
f.close()
copy.close()

Python copy file overwrite

Here, we can see how to copyfile and overwrite in python.

  • In this example, I have imported a module called shutil, and os. The shutil module helps in automating the process of coping and removing files and directories.
  • src = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\employee.txt’. is the path of the source file. The employee.txt is the filename that I have created with the .txt extension.
  • dst = r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\employee.txt’. is the path of the destination file. The employee.txt is the filename that I have created with the .txt extension.
  • shutil.move(src, dst) is used to copy the file from source to destination.
  • If the file is already present it will be moved to another directory, and then the file is overwritten. The shutil.move(src, dst) is used to move the file and overwrite. The work is the folder name.

Example:

import shutil
import os
src = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\employee.txt'
dst = r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\employee.txt'
shutil.move(src, dst)

Python check if a file is open

Here, we will see how to check the file is present or not by using os.system in python.

  • In this example, I have imported a module called os.path. This module is a path module for the operating system. It is used for the local paths.
  • The if os.path.isfile() is a method used to check whether the given files are present or not. The ‘kavita.txt’ is the filename. The else condition is used here to check if the condition is true, then it returns the file which is present else it will return the file not present.

Example:

import os.path
if os.path.isfile('kavita.txt'):
    print ("File present")
else:
    print ("File not present")

As the file is present, It returns a file is present in the output. You can refer to the below screenshot for the output.

Python check if a file is open with os.system
Python check if a file is open with os.system

Python check if a file is already opened

Here, we can see how to check if a file is already opened in python

In this example, I have opened a file called document using “wb+” mode which is used to read and write the file, as the file is created and it is already present it gives the output as the file is already opened. The ‘document’ is the file name.

Example:

file=open('document',"wb+")
if not file.closed:
    print("file is already opened")

To check the file is already opened, I have used print(“file is already opened”). You can refer to the below screenshot for the output.

Python check if a file is already opened
Python check if a file is already opened

Python check if a file exists

Now, we can see how to check if a file exists in python

  • In this example, I have imported a module called os.path. This module is a path module for the operating system, it is used for the local paths.
  • The path.exists() is a method that returns true if the file exists otherwise it returns false. Here, ‘document’ is the file name.

Example:

import os.path
from os import path
print(path.exists('document'))

To check whether the file exists or not, I have used print(path.exists(‘document’)). The below screenshot shows the output.

Python check if a file exists
Python check if a file exists

You may like the following Python tutorials:

In this Python tutorial, we have learned about the Copy file in Python. Also, We covered these below topics:

  • Python copy file to a folder
  • Python copy files from one location to another
  • Python copy file from one directory to another
  • Python copy file to USB
  • Python copy file content to another file
  • Python copy file and rename
  • Python copy file from subfolders to one folder
  • Python copy file to the current directory
  • Python copy file line by line
  • Python copy file overwrite