Skip to content
Related Articles

Related Articles

Improve Article

Python – Copy Files From Subfolders to the Main folder

  • Last Updated : 24 Jun, 2021

In this article, we will discuss how to copy a file from the subfolder to the main folder. The directory tree that will be used for explanation in this article is as shown below:

D:\projects\base
|
|__subfolder:
|    \__file.txt
|      
|__main.py
| 

Here we have a folder named “base” in which we have a folder named “subfolder” which contains a file name file.txt. We are going to copy the file.txt in the base folder from the subfolder.

Python comes with various modules that offer variety of ways to perform input/output operation and we will discuss them here.

The various methods that we will discuss includes:-



  1. Using shutil.copyfile()
  2. Using shutil.copy()
  3. Using shutil.copy2()
  4. Using shutil.copyfileobj()

Method 1: Using shutil.copyfile()

Using copyfile() method of shutil library we can easily copy a file from one location to other location. It takes 2 arguments the source path where the file that needs to be copied exist and the destination path where file is needed to be copied. Below is the code for implementation of this method:

Python




# importing the shutil module
import shutil
  
# storing the current path of file.txt
# in the source variable
source = 'D:/projects/base/subfolder/file.txt'
  
# storing the destination path in the
# destination variable
destination = 'D:/projects/base/file.txt'
  
# calling the shutil.copyfile() method
shutil.copyfile(source,destination)

After running this code we will notice that the file named file.txt has been copied to base folder.

Some points to be noted about shutil.copyfile() method:-

  • The destination location must be writable otherwise, an IOError exception will be raised.
  • If destination already exists, it will be replaced.
  • Special files such as character or block devices and pipes cannot be copied with this function.
  • It does not copy the metadata.

Method 2: Using shutil.copy()

Using the shutil.copy() method also we can copy the file the only change in syntax we can say is that the destination string that we pass can also be directory instead of full file path. For example see the code below:

Python




# importing the shutil module
import shutil
  
# storing the current file path of file.txt
# in the source variable
source = 'D:/projects/base/subfolder/file.txt'
  
# storing the destination directory in the
# destination variable
destination = 'D:/projects/base'
  
# calling the shutil.copy() method
shutil.copy(source,destination)

After running this code we will notice that the file named file.txt has been copied to base folder.



The main difference in copyfile() and copy() method are:

  • The copy() method also copies the information about permission but the copyfile() method does not.
  • The destination string can be a directory or full file path in copy() method but in copyfile() method it should be full file path.

Method 3: Using shutil.copy2()

The shutil.copy2() method is also like shutil.copy() method but the extra thing it does is it copies the metadata like time stamps.
The code will be similar to above:

Python




# importing the shutil module
import shutil
  
# storing the current path of file.txt 
# in the source variable
source = 'D:/projects/base/subfolder/file.txt'
  
# storing the destination path in the
# destination variable
destination = 'D:/projects/base'
  
# calling the shutil.copy2 method
shutil.copy2(source,destination)

After running this code we will notice that the file named file.txt has been copied to base folder.

Method 4: Using shutil.copyfileobj()

The shutil.copyfileobj() is also similar to above discussed method but the things different are firstly it takes file object instead of file paths and secondly is it takes an extra option argument which is the number of bytes kept in memory during the copy process. The default value is 16KB.

The code to copy using above method will be:

Python




# importing shutil module 
import shutil
    
# Source file
source = 'D:/projects/base/subfolder/file.txt'
    
# Open the source file
# in read mode and
# get the file object
fsrc = open(source, 'r'
    
    
# destination file
dest = 'D:/projects/base/file.txt'
    
# Open the destination file
# in write mode and
# get the file object
fdst = open(dest, 'w')
    
    
# Now, copy the contents of
# file object f1 to f2 
# using shutil.copyfileobj() method
shutil.copyfileobj(fsrc, fdst)
    
# We can also specify
# the buffer size by paasing
# optional length parameter
# like shutil.copyfileobj(fsrc, fdst, 1024)
  
    
# Close file objects
fdst.close()
fsrc.close()

After running this code we will notice that the file named file.txt has been copied to base folder.

We can also remember the difference between the above methods using the given table:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :