Python Files

Python File I/O

As a data scientist, you deal with a large amount of data on a regular basis. And this data could come from a variety of sources, including databases, Excel spreadsheets, flat files, and public websites such as Kaggle. Not just sources, but any file type such as .csv, .txt, .parquet, and so on. Before you can begin making sense of the data, you must first understand the basic Python file operations such as how to access, read, and write data into flat files so that you can do analytics on them.

Python, like other programming languages, enables file management and allows users to manipulate files. The concept of file handling has spread to many other languages, but the implementation is either complicated or lengthy. However, like other Python innovations, this concept is simple and short. This article will teach you about Python file operations and the basic I/O functions available in Python. More specifically, opening a file, reading from it, writing into it, and closing it, as well as other file techniques to be aware of.

Python File

Files are identified locations on the disc where associated data is stored. They are used to retain data in non-volatile memory indefinitely (e.g. hard disk). Because Random Access Memory (RAM) is volatile (it loses data when the computer is shut off), we employ files to store data for future use by permanently storing it.

When we wish to read or write to a file, we must first open it. When we’re finished, it needs to be closed so that the resources associated with the file may be released. As a result, Python file operations occur in the following order:

  • Opening a file
  • Reading or writing a file (perform operation)
  • Closing the file

In Python, there are two sorts of files that can be handled: text files and binary files (written in binary language- 0s and 1s)

  • Binary files – The majority of files you use on a regular basis on your computer are binary files, not text ones. That’s right, even though it only contains text, the Microsoft Word.doc file is a binary file. There is no line terminator in this form of a file, and the data is kept after it has been converted into machine-readable binary language.
  • Text files – A text file, on the other hand, does not require any special encoding and may be opened with any ordinary text editor. Each line of text in this sort of file is terminated with a special character known as EOL (End of Line), which is the new line character (‘\n’) in Python by default.

Opening Files in Python

To open a file, Python includes the open() function. This function returns a file object, often known as a handle, which is used to read or change the file. This function does not require the import of any modules.

The syntax of the Python open file function is as follows:

                    

File_object = open(filename, access_mode)

where,

  • filename – name of the file that the file object has to open
  • access_mode – attribute of the file that tells which mode a file was opened in. Default is reading in text mode

For example,

                    

>>> f = open('myFile.txt')    # opens file in current directory

>>> f = open('C:\MyFolder\myFile.txt')  # specifying full path

In this case, the filename is the name of the file with which you want to communicate, including the file extension. That is, if you have a text file named myFile.txt, the filename is not simply “myFile.” It’s called “myFile.txt“. If you’re using Windows, you can also specify the file’s actual location, such as “C:\MyFolder\myFile.txt“.

Note – The file must be in the same directory as the Python program file; otherwise, the full location of the file must be written in place of the filename.

When we open a file, we can define the access mode. Python understands what you want to do with the file based on the mode parameter in the open function. We specify whether we wish to read r, write w, or add a to the file in mode. We can also choose whether to open the file in text or binary mode. Reading in text mode is the default setting. When reading from a file in this mode, we get strings. Binary mode, on the other hand, returns bytes and is the mode to employ when dealing with non-text files such as images or executable files.

Mode

Description

r Allows you to open a file for reading. (default)
w This command opens a file for writing. If the file does not exist, it is created; otherwise, it is truncated.
x Opens a file for the purpose of exclusive creation. The operation fails if the file already exists.
a Opens a file for adding at the end without truncating it. If the file does not exist, it is created.
t Opens a file in text mode (default)
b Opens in binary mode.
+ Opens a file for modification (reading and writing)

One new mode has been added to Python 3:

‘x’Exclusive Creation Mode – This mode is used to generate a file only. The function call will fail if a file with the same name already exists.

Few examples of opening a file in different access modes,

                    

f = open('myFile.txt')        # equivalent to 'r' or 'rt'

f = open('myFile.txt','w')    # write in text mode

f = open('myFile.txt', 'r+')  # open a file for reading and writing

f = open('img.bmp','r+b')     # read and write in binary mode

Closing Files in Python

When we have completed our operations on the file, we must properly close it. Closing a file frees up system resources that were previously associated with the file for I/O purposes. This allows you to manage your resources effectively while writing software with space or memory limits.

Python’s close() function is used to do this. Python has a garbage collector that can be used to clean away unreferenced objects, but we should not rely on it to shut the file.

Syntax:

                    

File_object.close()

Example

                    

f = open('myFile.txt', encoding = 'utf-8')
# perform file operations
f.close()

This procedure is not completely risk-free. If an exception occurs while doing some operation on the file, the code departs without closing it. A try…finally block is a safer option.

                    

try:
   f = open('myFile.txt', encoding = 'utf-8')
   # perform file operations
finally:
   f.close()

This ensures that the file is correctly closed even if an exception is generated, causing the program flow to halt.

When you close a file, you no longer have access to it until you reopen it at a later time. Any attempt to read or write to a closed file object will result in a ValueError exception:

                    

>>> f = open('/tmp/myFile.txt', 'w')
>>> f.close()
>>> f.read()

Output

                    

Traceback (most recent call last):
File "<input>", line 1, in 
    f.read()
ValueError: I/O operation on closed file.

The with statement is the most effective technique to close a file. After the nested code block is finished, this keyword automatically closes the file.

                    

with open('myFile.txt', encoding = 'utf-8') as x:
   # perform file operations

If you do not use the with keyword or the fileobject.close() function, Python will close and destroy the file object automatically using the built-in garbage collector. As a result, it is advisable to utilize the with keyword to control when the file will be closed.

Writing to Files in Python

Files are useless if you can’t write data to them. To accomplish this, we can employ the Python write() function. This adds the characters you specify to the end of a file. Keep in mind that when you create a new file object, Python will create the file if it does not already exist. When making your first file, you should utilize either the a+ or w+ modes. We must exercise caution when using the w mode, as it will overwrite the file if it already exists. As a result, all previous data is deleted.

There are two ways to enter data into a file:

  1. Python write() –

    Inserts the string str1 into the text file on a single line.

                    

File_object.write(str1)

  1. Python writelines() –

    Each string is inserted into the text file for a list of string elements. Used to insert many strings at once.

                    

File_object.writelines(L) for L = [str1, str2, str3]

Example

                    

with open('Recipe.txt', 'w') as file:
        file.write('5 eggs\n')
        file.write('2 tsp baking powder\n')
        file.write('60g butter\n')
        file.write('2 cup sugar\n')

Output

                    

5 eggs
2 tsp baking powder
60g butter
2 cup sugar

If this code is executed, the program will create a new file named recipe.txt in the current directory if one does not already exist. It gets rewritten if it exists. Now if we want to add more ingredients to the file, we open the file in append mode.

                    

with open('Recipe.txt', 'a') as file:
       file.write('2 cup water\n')
       file.write('250ml skimmed milk\n')

Output

                    

5 eggs
2 tsp baking powder
60g butter
2 cup sugar
2 cup water
250ml skimmed milk

Reading Files in Python

To begin reading a file in Python, open the file you wish to read. Python requires you to specify the name and location of the file you want to open. To read a file in Python, we must first open it in reading r mode. To read data from a file, we can use one of three functions, which are as follows:

  1. Python read() –

    The fileobject.read(size) method is used to read the contents of a file. If no size option is specified, this method will read the full file and send it to the console as a string (in text mode) or as byte objects (in binary mode). The size parameter instructs the read method how many bytes to return to the display from the file.

                    

File_object.read([n])

Let us use the above Recipe.txt file to carry out the reading Python file operation.

Example

                    

>>> f = open('Recipe.txt','r',encoding = 'utf-8')
>>> f.read(10)   # reads the first 10 data
'5 eggs\n2 t'

>>> f.read(20)   # reads the next 20 data
'sp baking powder\n60g'

>>> f.read()       # reads the rest of the data till the end of file
' butter\n2 cup sugar\n2 cup water\n250ml skimmed milk\n'

  1. Python readline() –

    Data in a file can also be parsed by reading it line by line. This allows you to scan an entire file line by line, progressing only when necessary. Reads a line from a file and returns it as a string. Reads at most n bytes for the provided n. However, even if n is greater than the length of the line, does not read more than one line.

                    

File_object.readline([n])

Example

                    

>>> f = open('Recipe.txt','r',encoding = 'utf-8')
>>> f.readline()
'5 eggs\n'

>>> f.readline()
'2 tsp baking powder\n'

>>> f.readline()
'60g butter\n'

>>> f.readline()
'2 cup sugar\n'

  1. Python readlines() –

    The fileobject.readlines() method (note the plural), which produces a list of all the lines in the file. This method reads all the lines and returns them as string elements in a list, one for each line.

                    

File_object.readlines()

Example

                    

>>> f = open('Recipe.txt','r',encoding = 'utf-8')
>>> f.readlines()
['5 eggs\n', '2 tsp baking powder\n', '60g butter\n', '2 cup sugar\n', '2 cup water\n',
'250ml skimmed milk\n']

Python File Methods

To conduct various Python file operations with the help of file objects, you may use a variety of methods. Some of them were utilized in the above examples. Let us look at some more Python file operation methods below:

Method Description
close() This function closes an open file. If the file is already closed, it has no effect.
detach() Returns the underlying binary buffer after separating it from the TextIOBase.
fileno() The file’s integer number (file descriptor) is returned.
flush() Flushes the file stream’s write buffer.
isatty() If the file stream is interactive, this method returns True.
read(n) Reads a maximum of n characters from the file. If it is negative or None, it reads till the end of the file.
readable() If the file stream can be read from, this method returns True.
readline(n=-1) Reads one line from the file and returns it. If n is supplied, it reads in at most n bytes.
readlines(n=-1) Reads the file and returns a list of lines. If n bytes/characters are given, this function reads in at most n bytes/characters.
seek(offset,from=SEEK_SET) Changes the file position in reference to from to offset bytes (start, current, end).
seekable() If the file stream enables random access, this function returns True.
tell() The current file location is returned.
truncate(size=None) The file stream is resized to size bytes. If no size is supplied, it resizes to the current position.
writable() If the file stream can be written to, this method returns True.
write(s) Returns the number of characters written after writing the string s to the file.
writelines(lines) A list of lines is written to the file.

Frequently Asked Questions

Q1. How do you write to a file in Python?

Files are useless if you can’t write data to them. To accomplish this, we can employ the Python write() function. This adds the characters you specify to the end of a file. Keep in mind that when you create a new file object, Python will create the file if it does not already exist. When making your first file, you should utilize either the a+ or w+ modes. We must exercise caution when using the w mode, as it will overwrite the file if it already exists. As a result, all previous data is deleted.

There are two ways to enter data into a file:

  1. Python write() – Inserts the string str1 into the text file on a single line.

  1. Python writelines() – Each string is inserted into the text file for a list of string elements. Used to insert many strings at once.

                    

File_object.writelines(L) for L = [str1, str2, str3]

Q2. How do you open and write to a file in Python?

The open() method in Python allows you to write to a file. To write to a file, you must supply either “w” or “a” as an argument. “w” overwrites a file’s existing content, whereas “a” appends material to a file.

For example

                    

my_file = open('Recipe.txt', 'w')

We’ve used the w mode to open our Recipe.txt file. This means we can make changes to the file we’re working on. You can write to a file in this mode. It deletes the contents of a file and replaces it with a new one.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.