Python File Open
File handling is an important part of any web application.
Python has several functions for creating, reading, updating, and
deleting files.
Python File Operation
A file is a named location used for storing data. For example, main.py is a file
that is always used to store Python code.
Python provides various functions to perform different file operations, a
process known as File Handling.
Opening Files in Python
In Python, we need to open a file first to perform any operations on it—we use
the open() function to do so. Let's look at an example:
Suppose we have a file named file1.txt .
To open this file, we can use the open() function.
file1 = open("file1.txt")
Here, we have created a file object named file1 . Now, we can use this object
to work with files.
Mode on File Opening
1.Different File Opening Modes
Python allows us to open files in different modes (read, write, append, etc.),
based on which we can perform different file operations. For example,
file1 = open("file1.txt")
Here, we are opening the file in the read mode (we can only read the content,
not modify it).
Note: By default, Python files are open in read mode. Hence, the
code open("file1.txt", "r") is equivalent to open("file1.txt").
Different Modes to Open a File in Python
Mode Description
r Open a file in reading mode (default)
w Open a file in writing mode
x Open a file for exclusive creation
a Open a file in appending mode (adds content at the end of the file)
t Open a file in text mode (default)
b Open a file in binary mode
+ Open a file in both read and write mode
Here are a few examples of opening a file in different modes,
# open a file in default mode (read and text)
file1 = open("test.txt")
# equivalent to open("test.txt", "rt")
# open a file in write and text mode
file1 = open("test.txt",'w')
# open a file in read, write and binary mode
file1 = open("img.bmp",'+b')
2.Opening a File Using its Full Path
We can also open a file using its full path.
file_path = "/home/user/documents/file1.txt"
file1 = open(file_path)
In this example, /home/user/documents/file1.txt is a full path to a file
named file1.txt located in the /home/user/documents/ directory.
Open a File on the Server
Assume we have the following file, located in the same folder as
Python:
demofile.txt
Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!
To open the file, use the built-in open() function.
The open() function returns a file object, which has
a read() method for reading the content of the file:
Example
f = open("demofile.txt", "r")
print(f.read())
If the file is located in a different location, you will have to specify
the file path, like this:
Example
Open a file on a different location:
f = open("D:\\myfiles\\welcome.txt", "r")
print(f.read())
Read Only Parts of the File
By default the read() method returns the whole text, but you can
also specify how many characters you want to return:
Example
Return the 5 first characters of the file:
f = open("demofile.txt", "r")
print(f.read(5))
Read Lines
You can return one line by using the readline() method:
Example
Read one line of the file:
f = open("demofile.txt", "r")
print(f.readline())
By calling readline() two times, you can read the two first lines:
Example
Read two lines of the file:
f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())
By looping through the lines of the file, you can read the whole file,
line by line:
Example
Loop through the file line by line:
f = open("demofile.txt", "r")
for x in f:
print(x)
Close Files
It is a good practice to always close the file when you are done with
it.
Example
Close the file when you are finish with it:
f = open("demofile.txt", "r")
print(f.readline())
f.close()
Note: You should always close your files, in some cases, due to
buffering, changes made to a file may not show until you close the
file.
Python File Write / Create
Write to an Existing File
To write to an existing file, you must add a parameter to
the open() function:
"a" - Append - will append to the end of the file
"w" - Write - will overwrite any existing content
Example
Open the file "demofile2.txt" and append content to the file:
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())
Example
Open the file "demofile3.txt" and overwrite the content:
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
#open and read the file after the overwriting:
f = open("demofile3.txt", "r")
print(f.read())
Note: the "w" method will overwrite the entire file.
Create a New File
To create a new file in Python, use the open() method, with one of the following
parameters:
"x" - Create - will create a file, returns an error if the file exist
"a" - Append - will create a file if the specified file does not exist
"w" - Write - will create a file if the specified file does not exist
Example
Create a file called "myfile.txt":
f = open("myfile.txt", "x")
Result: a new empty file is created!
Example
Create a new file if it does not exist:
f = open("myfile.txt", "w")
File Copy & Delete Operation
Python provides various methods to perform operations on the files and
folders of the underlying operating system.
The OS module in Python has functions for adding and deleting folders,
retrieving their contents, changing the directory, locating the current
directory, and more. Import this module, we will use the listdir()
method of it to fetch the files.
Similarly, the shutil module provides a number of functions for dealing
with operations on files and associated collections. It gives users the
option to copy and delete files. You can copy the contents of one folder
to another using the shutil.copy(), shutil.copy2(),
and shutil.copytree() methods of this module.
You can include these functions in your file by importing their respective
modules as shown below −
import shutil
shutil.submodule_name(arguments passed)
Using shutil.copy() operation
Using this function, the text or content of the source file is copied to the
target file or directories. Additionally, the permission mode of the file is
preserved, but the file metadata (such as the "Date Creation", "Date
Modification" etc.) is not preserved.
Syntax
Following is the syntax of the shutil.copy() method
shutil.copy(origin, target)
Where,
Origin − A string containing the source file's location or path
Target − A string containing the destination file's location or path.
Example
Following is an example of to copy files from one folder to other
using shutil.copy() operation –
# importing the modules
import os
import shutil
# Providing the folder path
origin = 'C:\Users\Lenovo\Downloads\Works\'
target = 'C:\Users\Lenovo\Downloads\Work TP\'
# Fetching the list of all the files
files = os.listdir(origin)
# Fetching all the files to directory
for file_name in files:
shutil.copy(origin+file_name, target+file_name)
print("Files are copied successfully")
Output
Following is an output of the above query:
Files are copied successfully
Using shutil.copy2() operation
First of all, this function is exactly like copy() with the exception that it
keeps track of the source file's metadata.
The execution program for this is exact same as shutil.copy(). The only
difference is that while fetching the file to directory, in place
of shutil.copy() we write shutil.copy2().
shutil.copy2(origin+file_name, target+file_name)
Syntax
Following is the syntax of the shutil.copy2() method
shutil.copy2(origin, target)
Origin and target values are same as defined above.
The copy2() function in this code does one additional operation in addition to
a copy() which is to keep the metadata.
Using shutil.copytree() method
This function moves a file and any subdirectories it contains from one
directory to another.
This indicates that both the source and the destination include the file. The
string must contain the names of both parameters.
Syntax
Following is the syntax of the shutil.copytree() method –
shutil.copytree(origin, target)
Origin and target values are same as defined above.
Example
Following is an example of to copy files from one folder to other
using shutil.copytree() operation:
# importing the module
import shutil
# Fetching all the files to directory
shutil.copytree('C:\Users\Lenovo\Downloads\Works','C:\Users\Lenovo\
Downloads\Work TP\/newfolder')
print("File Copied Successfully")
Output
Following is an output of the above query:
File Copied Successfully
As an output we will be able to see the changes made after the execution i.e.
the ‘Works’ folder gets copied to the 'Works TP' folder with the name of
'newfolder' as assigned in the code above containing all the files inside it
which was there in the Works folder.
In order to obtain a duplicate of that file, we have included
the copytree() function in this code.
How to copy certain files from one
folder to another using Python?
Copy All Files
Let's start with a basic example where we copy all files from a source folder to
a target folder. We will use the os module to iterate through the files in the
source folder and the shutil module to perform the actual copying.
Example
In this example, we define a function copy_all_files that takes the paths of the
source and target folders as arguments. We use os.listdir() to obtain a list of
all files in the source folder and then iterate through them. For each file, we
construct its full path using os.path.join(). The os.path.isfile() function checks if
the item is a file (not a directory). If it is a file, we use shutil.copy() to copy it to
the target folder.
import os
import shutil
def copy_all_files(source_folder, target_folder):
for filename in os.listdir(source_folder):
source_file = os.path.join(source_folder, filename)
if os.path.isfile(source_file):
shutil.copy(source_file, target_folder)
# Example usage
copy_all_files('source_folder', 'target_folder')
Copy Files with Specific Extensions
Sometimes, we might only want to copy files with certain extensions. For
instance, we might want to copy only .txt files or .jpg images. Let's modify our
previous example to achieve this:
Example
In this modified version, we added an additional extension parameter to the
function. We then check if each file's name ends with the specified extension
before copying it to the target folder.
import os
import shutil
def copy_files_with_extension(source_folder, target_folder, extension):
for filename in os.listdir(source_folder):
source_file = os.path.join(source_folder, filename)
if os.path.isfile(source_file) and filename.endswith(extension):
shutil.copy(source_file, target_folder)
# Example usage
copy_files_with_extension('source_folder', 'target_folder', '.txt')
Copy Files Based on Filename Patterns
What if we want to copy files based on specific patterns in their names? For
instance, we may want to copy all files starting with "report_" or ending with
"_final." We can achieve this using the glob module in Python:
Example
In this example, we define a function copy_files_with_pattern that takes the
source folder, target folder, and the desired pattern as arguments. We use
glob.glob() to obtain a list of file paths that match the specified pattern in the
source folder.
import shutiltarget folder.
import os
import shutil
def copy_all_files(source_folder, target_folder):
for filename in os.listdir(source_folder):
source_file = os.path.join(source_folder, filename)
if os.path.isfile(source_file):
shutil.copy(source_file, target_folder)
# Example usage
copy_all_files('source_folder', 'target_folder')
Copy Files with Specific Extensions
Sometimes, we might only want to copy files with certain extensions. For
instance, we might want to copy only .txt files or .jpg images. Let's modify our
previous example to achieve this:
Example
In this modified version, we added an additional extension parameter to the
function. We then check if each file's name ends with the specified extension
before copying it to the target folder.
import os
import shutil
def copy_files_with_extension(source_folder, target_folder, extension):
for filename in os.listdir(source_folder):
source_file = os.path.join(source_folder, filename)
if os.path.isfile(source_file) and filename.endswith(extension):
shutil.copy(source_file, target_folder)
# Example usage
copy_files_with_extension('source_folder', 'target_folder', '.txt')
import glob
def copy_files_with_pattern(source_folder, target_folder, pattern):
for file_path in glob.glob(os.path.join(source_folder, pattern)):
if os.path.isfile(file_path):
shutil.copy(file_path, target_folder)
# Example usage
copy_files_with_pattern('source_folder', 'target_folder', 'report_*')
Copy Files Modified within a Date Range
At times, we might need to copy files that were modified within a specific date
range. For instance, we may want to copy all files modified in the last 7 days.
Let's explore how we can accomplish this:
Example
In this example, we define a function copy_files_within_date_range that takes
the source folder, target folder, and the number of days as arguments. We
calculate the cutoff time by subtracting the number of days in seconds from
the current time using time.time(). We then check if each file's modification
time (os.path.getmtime()) is greater than or equal to the cutoff time before
copying it to the target folder.
import shutil
import os
import time
def copy_files_within_date_range(source_folder, target_folder, days):
cutoff_time = time.time() - days * 86400
for filename in os.listdir(source_folder):
source_file = os.path.join(source_folder, filename)
if os.path.isfile(source_file) and os.path.getmtime(source_file) >=
cutoff_time:
shutil.copy(source_file, target_folder)
# Example usage to copy files modified within the last 7 days
copy_files_within_date_range('source_folder', 'target_folder', 7)
Copy Files Based on File Size
Lastly, let's explore how to copy files based on their sizes. We might want to
copy all files larger than a certain size or files within a specific size range.
Here's how we can implement it:
Example
In this final example, we define a function copy_files_within_size_range that
takes the source folder, target folder, minimum size, and maximum size as
arguments. We use os.path.getsize() to obtain the size of each file in bytes
and then check if the size falls within the specified range before copying the
file to the target folder.
import shutil
import os
def copy_files_within_size_range(source_folder, target_folder, min_size,
max_size):
for filename in os.listdir(source_folder):
source_file = os.path.join(source_folder, filename)
if os.path.isfile(source_file):
file_size = os.path.getsize(source_file)
if min_size <= file_size <= max_size:
shutil.copy(source_file, target_folder)
# Example usage to copy files between 1 MB and 10 MB
copy_files_within_size_range('source_folder', 'target_folder', 1000000,
10000000)
In this exhaustive article, we explored different approaches to copying
specific files from one folder to another using Python. We started with a basic
example that copies all files and then progressed to more specialized cases
where we copied files based on extensions, filename patterns, modification
dates, and file sizes. Python's standard library modules, such as os, shutil,
and glob, proved to be powerful tools for achieving these tasks. Armed with
this knowledge, you can now confidently manage file−copying operations in
your Python projects with ease. So you go ahead, harness the power of
Python, and master the art of file manipulation!
Python - Renaming and Deleting
Files
Python os module provides methods that help you perform file-processing
operations, such as renaming and deleting files.
To use this module, you need to import it first and then you can call any
related functions.
rename() Method
The rename() method takes two arguments, the current filename and the
new filename.
Syntax
os.rename(current_file_name, new_file_name)
Example
Following is an example to rename an existing file "test1.txt" to "test2.txt" −
#!/usr/bin/python3
import os
# Rename a file from test1.txt to test2.txt
os.rename( "test1.txt", "test2.txt" )
remove() Method
You can use the remove() method to delete files by supplying the name of the
file to be deleted as the argument.
Syntax
os.remove(file_name)
Example
Following is an example to delete an existing file "test2.txt" −
#!/usr/bin/python3
import os
# Delete file test2.txt
os.remove("text2.txt")
Check if File exist:
To avoid getting an error, you might want to check if the file exists
before you try to delete it:
Example
Check if file exists, then delete it:
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
Delete Folder
To delete an entire folder, use the os.rmdir() method:
Example
Remove the folder "myfolder":
import os
os.rmdir("myfolder")
Python Directory and Files
Management
A directory is a collection of files and subdirectories. A directory inside a
directory is known as a subdirectory.
Python has the os module that provides us with many useful methods to work
with directories (and files as well).
Get Current Directory in Python
We can get the present working directory using the getcwd() method of
the os module.
This method returns the current working directory in the form of a string. For
example,
import os
print(os.getcwd())
# Output: C:\Program Files\PyScripter
Here, getcwd() returns the current directory in the form of a string.
Changing Directory in Python
In Python, we can change the current working directory by using
the chdir() method.
The new path that we want to change into must be supplied as a string to this
method. And we can use both the forward-slash / or the backward-slash \ to
separate the path elements.
Let's see an example,
import os
# change directory
os.chdir('C:\\Python33')
print(os.getcwd())
Output: C:\Python33
Here, we have used the chdir() method to change the current working
directory and passed a new path as a string to chdir().
List Directories and Files in Python
All files and sub-directories inside a directory can be retrieved using
the listdir() method.
This method takes in a path and returns a list of subdirectories and files in that
path.
If no path is specified, it returns the list of subdirectories and files from the
current working directory.
import os
print(os.getcwd())
C:\Python33
# list all sub-directories
os.listdir()
['DLLs',
'Doc',
'include',
'Lib',
'libs',
'LICENSE.txt',
'NEWS.txt',
'python.exe',
'pythonw.exe',
'README.txt',
'Scripts',
'tcl',
'Tools']
os.listdir('G:\\')
['$RECYCLE.BIN',
'Movies',
'Music',
'Photos',
'Series',
'System Volume Information']
Making a New Directory in Python
In Python, we can make a new directory using the mkdir() method.
This method takes in the path of the new directory. If the full path is not
specified, the new directory is created in the current working directory.
os.mkdir('test')
os.listdir()
['test']
Renaming a Directory or a File
The rename() method can rename a directory or a file.
For renaming any directory or file, rename() takes in two basic arguments:
the old name as the first argument
the new name as the second argument.
Let's see an example,
import os
os.listdir()
['test']
# rename a directory
os.rename('test','new_one')
os.listdir()
['new_one']
Here, 'test' directory is renamed to 'new_one' using
the rename() method.