Python - Copy Files From Subfolders to the Main folder - GeeksforGeeks

15 Oct, 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:-
Using shutil.copyfile()Using shutil.copy()Using shutil.copy2()Using shutil.copyfileobj()
Using shutil.copyfile()
Using shutil.copy()
Using shutil.copy2()
Using shutil.copyfileobj()
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 moduleimport shutil # storing the current path of file.txt# in the source variablesource = 'D:/projects/base/subfolder/file.txt' # storing the destination path in the# destination variabledestination = 'D:/projects/base/file.txt' # calling the shutil.copyfile() methodshutil.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.
 
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 moduleimport shutil # storing the current file path of file.txt# in the source variablesource = 'D:/projects/base/subfolder/file.txt' # storing the destination directory in the# destination variabledestination = 'D:/projects/base' # calling the shutil.copy() methodshutil.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.
 
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 moduleimport shutil # storing the current path of file.txt# in the source variablesource = 'D:/projects/base/subfolder/file.txt' # storing the destination path in the# destination variabledestination = 'D:/projects/base' # calling the shutil.copy2 methodshutil.copy2(source,destination)
 
 
After running this code we will notice that the file named file.txt has been copied to base folder.
 
 
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 moduleimport shutil   # Source filesource = 'D:/projects/base/subfolder/file.txt'   # Open the source file# in read mode and# get the file objectfsrc = open(source, 'r')      # destination filedest = 'D:/projects/base/file.txt'   # Open the destination file# in write mode and# get the file objectfdst = open(dest, 'w')      # Now, copy the contents of# file object f1 to f2# using shutil.copyfileobj() methodshutil.copyfileobj(fsrc, fdst)   # We can also specify# the buffer size by passing# optional length parameter# like shutil.copyfileobj(fsrc, fdst, 1024)    # Close file objectsfdst.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:
 
 
sagartomar9927
Picked
Python directory-program
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()