Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

File Introduction to File Operations in Python _ What Are File Operations

This document provides a comprehensive guide on file operations in Python, covering essential tasks such as opening, reading, writing, and closing files. It explains various file modes, cursor positioning methods, and includes practical examples to illustrate each operation. The article serves as a valuable resource for beginners looking to enhance their file handling skills in Python.

Uploaded by

sammikumari1987
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

File Introduction to File Operations in Python _ What Are File Operations

This document provides a comprehensive guide on file operations in Python, covering essential tasks such as opening, reading, writing, and closing files. It explains various file modes, cursor positioning methods, and includes practical examples to illustrate each operation. The article serves as a valuable resource for beginners looking to enhance their file handling skills in Python.

Uploaded by

sammikumari1987
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Introduction

[Journey To Success]to File


Get yourOperations in Python
Personalized Learning Roadmap!! Download Now 03 D 05 H 09 M 14 S ×
Home

Rahul Shah — Updated On April 4th, 2023


Beginner Programming Python

In Python, file operations are essential for reading and writing data to files, and they play a
crucial role in data manipulation, analysis, and storage. In this article, we’ll explore the basics
of file operations in Python, including how to open, read, write, and manipulate files, along
with some advanced techniques to handle files efficiently. We’ll also discuss different file
modes, file objects, and file handling best practices that will help you to work with files in
Python like a pro.

This article was published as a part of the Data Science Blogathon!

Table of contents
What are File Operations in Python?
Opening a File
File Opening Modes in Python
Reading a File Operation in Python
Writing/Creating a File
Closing a File in Python
Cursor Positioning Methods
Truncating a File
Renaming a File
Deleting a File
Extras: The Encoding Argument
Extras: File Handling using Try-Except Blocks
A Practical Example
Conclusions
Frequently Asked Questions

What are File Operations in Python?


A file is an essential data item stored in one’s computer. Each file can be characterized with its
filename & file extension. Python programming language is capable of doing wonders and
with time we see a lot of its applications in various domains. One of its wonders handling and
organization of files which would save a lot of time for us.

A wide range of functions in Python is capable to cater the need of file operations such as
opening, reading, writing, creating files et cetera. In the following guide, we will go through
the basic yet most necessary operations which are highly useful during every file handling
task.

For performing file operations, a file needs to be opened first. Then follows the necessary
operations to betoperformed
Introduction by the user
File Operations on our file. After all the desired operations are
in Python
performed, the file needs to be closed. Closing a file is necessary as it would save the changes
made on our file from the current session.

List of File Operations in Python


Method Description
Opens a file and returns a file object. The filename argument is a
string that specifies the name of the file to open, and the mode
open(filename, mode)
argument specifies the mode in which to open the file (e.g. ‘r’ for read
mode, ‘w’ for write mode, etc.).
Closes the file object. Any further operations on the file object will
close() raise a ValueError. It’s a good practice to always close files after they
have been opened.
Reads and returns a string from the file. If size is specified, at most
read(size=-1) size characters will be read. If size is not specified or is negative, the
entire file will be read.
Reads and returns a single line from the file. If size is specified, at
readline(size=-1) most size characters will be read from the line. If size is not specified
or is negative, the entire line will be read.
Reads and returns a list of lines from the file. If hint is specified, at
readlines(hint=-1) most hint bytes will be read. If hint is not specified or is negative, all
the lines will be read.
Writes the string to the file. Returns the number of characters
write(string)
written.
Writes a sequence of strings to the file. The sequence can be any
writelines(sequence)
iterable object (e.g. a list, a tuple, etc.).
Changes the file position to the given offset. The whence argument is
seek(offset[, optional and defaults to 0, which means the offset is relative to the
whence]) beginning of the file. Other values for whence are 1 (relative to the
current position) and 2 (relative to the end of the file).
tell() Returns the current file position.
Truncates the file to at most size bytes. If size is not specified, the
truncate(size=None)
entire file will be truncated.
Flushes the write buffer of the file. This is useful when writing to a file
flush()
that is being accessed by multiple processes or threads.

Opening a File
Opening a file is the fundamental step in every file handling task. This makes sense because,
in any file explorer, we first open before performing any reading or writing operations to it.

Files in Python can be opened with a built-in open() function. Ideally, it takes two string
arguments:

The file path including the file name and the extension we want to open, is to be passed
as a string
The mode in which we want to open the file, to be passed as a string.
Thus, the syntax for opening a file would look like this:
DataHour: Building Chatbots using LLM
Introduction to File Operations Date:
in Python 🗓️
25 Oct 2023 🕖
Time: 7:00 PM – 8:00 PM IST

RSVP!

1. open(“”, “”)
Indeed there are other arguments as well in the open() function which are optional and
are used as per the requirement of the user. Let’s look at an example for opening a file.
Suppose if we have a file named myfile.txt on our desktop, it can be opened by:

2. open(“C:UsersRahulDesktopmyfile.txt”)
Unquestionably, if our current working directory of the Python file is the same as our
file (here desktop), there is no need to specify the full path. In such a case, our open()
function would look like:

3. open(“myfile.txt”)
For the ease of code implementation, let us consider our current working directory the
same as the location our text files are present for the rest of the article.
Python has a wide range of file opening modes available by default. These modes specify in
which mode our file needed to be opened. Each file opening mode comes with its default
functionality and one can select the appropriate model based on the need. As discussed
above, the file opening mode argument is an important factor that decides whether we want
to read, write or update the contents of an existing file.

Different Ways of Loading Data using


Python
This article was published as a part of the Data Science Blogathon.
Introduction When we started our data science journey the very first
library that got our way was pandas and the very first function was
read_csv(). But do we have only a CSV file to read? No right! This article
will let you know … Continue reading

Analytics Vidhya 1
File Opening Modes in Python
Mode Description Example
file =
Introduction toOpens
Read mode. File Operations in Python
the file for reading (default mode). If the
r open('example.txt',
file doesn’t exist, an error will be raised.
'r')
file =
Write mode. Opens the file for writing. If the file exists, it
w open('example.txt',
will be truncated. If the file doesn’t exist, it will be created.
'w')
Append mode. Opens the file for writing, but appends new file =
a data to the end of the file instead of overwriting existing open('example.txt',
data. If the file doesn’t exist, it will be created. 'a')
Exclusive creation mode. Opens the file for writing, but only file =
x if it doesn’t already exist. If the file exists, an error will be open('example.txt',
raised. 'x')
file =
Binary mode. Opens the file in binary mode instead of text
b open('example.txt',
mode.
'rb')
file =
Text mode (default). Opens the file in text mode instead of
t open('example.txt',
binary mode.
'rt')
file =
+ Update mode. Opens the file for both reading and writing. open('example.txt',
'r+')

The modes discussed above are being used on a text file. To use these modes for a binary file,
we need to use a different combination of file opening mode arguments. Using ‘b’ with any
mode, For example, ‘ab’, ‘rb’, ‘wb’, ‘rb+’, the file opens in binary mode. It is used for non-textual
files like image, sound, executable (.exe) files.

For Example, To open our file myfile.txt in b mode:

open("myfile.txt", "rb")

Opening a file is not enough. This open function needs to be stored into a variable as this will
be used later for writing & reading the file. Here we will assign a variable to our file:

file = open("myfile.txt", "r")

Now that we have learned to open a file, we will look at how to read from a file.
Recommended For You
Reading a File Operation in Python Become a full stack data
Now that we have opened a file, we can start performing operations to it. In this section, we scientist

will look at the reading functionality of files using Python. Before start reading the file, as
PyPDF2 Library for Working
discussed, we must open the file in ‘r’ mode, since we are going to read the contents of the with PDF Files in Python

file. Let us assume that our file myfile.txt already has some textual data present in it. Automate Everything With
Python: A Comprehensive Guide
to Python Automation

Python CSV Quick & Simple


Guide | Read, Write &
Manipulate (Updated 2023)

How to Read and Write With


CSV Files in Python?
Apache Airflow: How to
Dynamically Fetch Data and
Email?

Introduction to File Operations in Python Using AWS S3 with Python


boto3

On executing this code, we get:

Source – Personal Computer

The .read() method to our variable file gives the content of the file as output. We can also
specify the number of characters we want to read at once.

file = open("myfile.txt", "r")


print(file.read(8))

On executing this code, we get:

Source – Personal Computer

This will print the 8 characters from the point until the file has been read earlier.

Since we executed the open statement before the .read() method, the file is opened again and
the cursor is moved at the start point, i.e. index 0. Thus 8 characters are being read from the
start point.

Let’s understand this with another example. Given our file named myfile.txt having some
content “Hello, how you all have been doing?”. After opening a file, if we use
print(file.read(6)), it will first give “Hello,” as output & if use print(file.read(8)), it will start
reading 8 characters file from the point where the first read function left off i.e. ” how you”

file = open("myfile.txt", 'r')


print(file.read(6))
print(file.read(8))

On executing this code, we get:

Source – Personal Computer


As a result, when all the characters of the file are being read & if we try to use the .read()
method again, it will give an empty string as output.

The .readline() method


Introduction to Fileprints each lineinfrom
Operations our file, every time we execute this function,
Python
until the end of the file is reached. Let’s add some additional lines to our text file to illustrate
this example.

file = open("myfile.txt", 'r')


print(file.readline())
print(file.readline())

On executing this, we get:

Source – Personal Computer

This will print the first two lines of our file, separated by a newline character.

The .readlines() method always gives a list of all the lines of our file as list elements.

file = open("myfile.txt", 'r')


print(file.readlines())

On executing this we get:

Source – Personal Computer

A major difference between the .read() method and the .readline() method is, .read() method
focuses on reading each character from a file while the .readline() focuses on reading
individual lines from a file.

Now let’s look at another important yet vital file operation i.e. writing and creating a file.

Writing/Creating a File
Another useful operation widely used, after opening a file, is writing into a file. Writing into a
file in Python is easy but an attentive task. Writing into a file is typically done by opening the
file in write ‘w’ or append ‘a’ mode. Writing into a file can be done using the .write() method to
our file. Caution needed to be followed while using the ‘w’ write mode as it can erase existing
content present in the file.

Let’s understand this with an example. If we want to add content to our existing file,
myfile.txt, first we need to open it in the ‘w’ write mode and use the .write() method on our
file. The content to be written into the file needs to be passed as a string. It’s a good practice
to add a newline character ‘n’ to add a line between sentences. This improves the readability
for the user and is also useful while using the .readline() and .readlines() methods to
distinguish between the lines.

file = open("myfile.txt", "w")


file.write("Hello Alln")
On executing this code, we get:

Introduction to File Operations in Python

Source – Personal Computer

Here, on execution, the .write() method returns the number of characters written into the
file.

While writing into a file, if the file opened in ‘w’ or ‘w+‘ mode does not exist, a new file will be
created in the same name in our present working directory.

For Example:

file = open("file.txt", "w")

On executing this code, we get:

Source – Personal Computer

Here, previously the file file.txt does not exist in the current working directory and later it got
created. If file.txt would have already existed, it will erase all the content and the file will
become empty.

Another file mode used for writing to the file is the ‘a’ append mode. This creates a new file if
the specified file does not exist in the current working directory. Unlike ‘w’ mode, it does not
remove the existing content from the file. Instead, it appends or adds the new content at the
end of the file. Writing in append mode can be done using the .write() method.
Example:

file = open("myfile.txt", "a")


file.write("I am using append moden")

On executing this code, we get:

Source – Personal Computer

This will append the new content at the end of existing into our specified file.

Now, let’s look at another vital file operation, how to close a file.

Closing a File in Python


At last, after opening and performing the reading, writing operations, it is important to close
the file. This is done using .close() method. Let’s understand this with an example:

file = open("myfile.txt", 'r')


print(file.read())
file.close()
Introduction to File Operations in Python
Source – Personal Computer

It is always a good practice to close a file after performing desired operations to it. Thus
makes sure that all our changes have been saved to the file. In the later sections, where we
will try to rename and remove the files. To accomplish these tasks, we must close the files to
permit renaming and removing the files.

Cursor Positioning Methods


Python has a couple of essential methods for the positioning of the cursor in our file. The
methods are as follows:

1. The .seek() method

The .seek() method in Python is used to change the cursor to a specific position.

file = open("myfile.txt", 'r')


file.seek(0)
file.close()

This will move our cursor to index position 0 & file reading will start from the start point
again.

2. The .tell() method

The .tell() method in Python prints the current position of our cursor.

file = open("myfile.txt", 'r')


file.tell()

This will give the position up to which file has been read.

Source – Personal Computer

Since we have used the .seek(0) method previously to move the cursor at index position 0, we
got 0 as output for using the .tell() method.
Now, let’s look at some additional yet essential methods which might come in handy in file
handling tasks.

Truncating a File
Truncating a File is also possible in Python. By using the .truncate() method, we can truncate
the file up to the desired length. Let’s understand this with an example:

file = open('myfile.txt', 'w')


file.truncate(20)
file.close()
Source – Personal Computer
In this example, we truncated a file up to 20 bytes by passing 20 as an argument into the
.truncate() method. This reduced the file size of the file to 20 bytes. Reducing the file size also
Introduction to FileofOperations
reduces the contents the file size. in
If Python
we don’t specify the size parameter inside the
.truncate() method, the file will get truncated up to the current cursor position of the file.

A point to note in the .truncate() method is, the file must be opened in write mode to perform
truncating task.

Renaming a File
Renaming a file in Python can be done using the os Module and needed to be imported to
perform such tasks. The os module in Python has a vast collection of methods to perform all
the essential file management tasks in Python itself.

To rename our file, we will use the .rename() method from the os module. The .rename()
method takes two arguments:

The current file name, passed as a string type.


The renamed file name, to be passed as a string type.
Let’s understand this with an example.

import os
os.rename('myfile.txt', 'ourfile.txt')

Source – Personal Computer

Here, ‘myfile.txt’ is our file’s current name and ‘ourfile.txt’ is the name we want to set.

Since we are just renaming the file and not changing the content of the file, we do not need to
open it to perform the renaming task.

Deleting a File
We can remove the files from a directory using Python’s versatile os module. The os module
has a method .remove() which performs the task. The .remove() method takes a single
argument as string type which is our file name. Let’s understated this with an example.

import os
os.remove('myfile.txt')
file = open('myfile.txt', 'r')

On executing this, we get:


Source – Personal Computer
Since we deleted this file, we cannot open this again and as a result, getting errors.

Introduction to File Operations in Python


Extras: The Encoding Argument
While opening the file, it may happen that we do not get the desired result or we might get an
abnormal result while reading the file. This may happen due to encoding issues in the file. The
default encoding used for text files is cp1252 in Windows Operating System.
Thus, it is always good to play safe and use an encoding argument while opening the file.

For Example:

open("myfile.txt", "r", encoding = "cp1252")

Extras: File Handling using Try-Except Blocks


Often one forgets to close the file. This may produce errors and may become harmful when
you are working on a very big file. In such scenarios, try-except-finally blocks come to the
rescue.
We can add the file close method into the finally block so that even if the program execution
stops due to an exception, the file will get closed anyway.
Example:

try:
file = open("myfile.txt")
finally:
file.close()

Thus, if in any case, an exception is thrown in the try block, finally block will be executed and
thus file will be closed.

A Practical Example
Now we understood all the operations individually, we must now understand how these
functions work together in a general file management task. In the following example, we will
understand how these methods are used and in which order would help one performing any
file operation task on its own. An important thing to note is every time before shifting from
writing into a file to read from file and vice versa, one must close the current file opened in a
specific mode & open it again in the mode we want. Now, let us move to the example:

file = open("myfile.txt", 'w')


file.write("This is the Practical ExamplenIt performs reading, writing and closing
operations of filen")
file.close()
file = open("myfile.txt", 'r')
print(file.read(10))
file.seek(0)
print(file.readlines())
file.seek(0)
print(file.readline())
file.close()

On executing this code, we get:


Source – Personal Computer

Introduction to File Operations in Python


Conclusions
In this guide, we learnt about file operations in Python. We started with the basic features of
a file and covering major operations involved in a file. These major operations involve
reading, writing, creating and closing the file. We also looked at cursor positioning operations
in a file using the .seek() and .tell() methods. We also looked at few more additional
operations in file handling such as renaming, truncating and removing a file using Python
built-in functions and os module methods. Later we looked at few extra parameters and a
different way of implementing file handling tasks using try-except blocks. At last, we looked
at a practical example to understand how all these methods work together in any general
task.

File Operation in Python is a great asset for accessing and manipulating files directly into
your Python program. Unlike other programming languages, file handling in Python is
uncomplicated & pretty straightforward and as already said, can help you save a lot of time.
With that said, one can try exploring a few more operations and methods associated with file
handling to make their tasks more efficient and productive.

Frequently Asked Questions


Q1. What is a file handling in Python?
A. File handling in Python is the process of reading from and writing to files using the built-in
file objects. It allows developers to work with files to store and manipulate data.

Q2. What are the types of file in file handling in Python?


A. The two main types of files in file handling in Python are text files and binary files. Text files
store textual data in ASCII or Unicode format, while binary files store non-textual data such
as images or audio files.

Q3. How do you write file handling in Python?


A. To write file handling in Python, you can use the built-in open() function to create a file
object, and then use the appropriate file methods to perform read/write operations. Once
done, you should close the file using the close() method

Q4. What are the file operations in Python?


A. The file operations in Python include opening a file, reading from a file, writing to a file,
appending to a file, seeking a specific position in a file, and closing a file. These operations
allow developers to manipulate files and their contents in a variety of ways.

Q5. How many types of file operations are there in Python?


A. There are several types of file operations in Python, including read, write, append, seek,
flush, and truncate. These operations provide a wide range of functionality for working with
files, making it easy to store, retrieve, and manipulate data stored in files.

The media shown in this article is not owned by Analytics Vidhya and are used at the
Author’s discretion!
blogathon File Operations in Python

Introduction to File Operations in Python

About the Author


Rahul Shah
IT Engineering Graduate currently pursuing Post Graduate Diploma in
Data Science.

Our Top Authors

view
more

Download
Analytics Vidhya App for the Latest blog/Article

Previous Post Next Post

A Comprehensive Guide on Feature Getting started with the basic tasks of


Engineering Computer Vision
Leave a Reply
Introduction to File Operations in Python
Your email address will not be published. Required fields are marked *

Comment

Name* Email*

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

Submit

Top Resources

10 Best AI Image How to Read and Write Understand Random Everything you need to
Generator Tools to Use With CSV Files in Forest Algorithms With Know about Linear
in 2023 Python? Examples (Updated Regression!
2023)
avcontentteam - Harika Bonthu - KAVITA MALI -
AUG 17, 2023 AUG 21, 2021 Sruthi E R - JUN 17, 2021 OCT 04, 2021

Analytics Vidhya Data Scientists Companies Visit us


About Us Blog Post Jobs

Our Team Hackathon Trainings

Careers Join the Community Hiring Hackathons


Download App Contact us Apply Jobs Advertising

© Copyright 2013-2023 Analytics Vidhya. Privacy Policy Terms of Use Refund Policy

You might also like