Chat Zalo Chat Messenger Phone Number Đăng nhập
How to read from a file in Python - GeeksforGeeks

How to read from a file in Python – GeeksforGeeks

Python provides built-in functions for creating, writing, and reading files. There are two types of files that can be handled in python, normal text files and binary files (written in binary language, 0s and 1s).

Text files: In this type of file, each line of

  • text ends with a special character called EOL (End of Line), which is the new line character (‘n’) in python by default
  • .

  • Binary files: In this file type, there is no terminator for a line and the data is stored after being converted into a machine-readable binary language.

Note: To learn more about file handling, click here.

Access mode

Access modes govern the type of operations possible on the open file. It refers to how the file will be used once it is opened. These modes also define the location of the file handle in the file. The file handle is like a cursor, which defines where data should be read or written to the file from. The different access modes to read a file are –

  1. Read Only (‘r’): Open text file to read. The driver is placed at the beginning of the file. If the file does not exist, it generates an I/O error. This is also the default mode in which the file is opened.
  2. Read and write

  3. (‘r+’): Opens the file to read and write. The driver is placed at the beginning of the file. Generates an I/O error if the file does not exist.
  4. Append and Read (‘a+’): Opens the file to read and write. The file is created if it does not exist. The driver is placed at the end of the file. The data that is written will be inserted at the end, after the existing data.

Note: To learn more about the access mode click here.

Opening a file

It is done using the open() function. You do not need to import any modules for this function.

Syntax:

File_object = open(r”File_Name”, “Access_Mode”)The file must exist in the

same directory as the python program file, otherwise the full address of the file must be written instead of the file name. note: The r is placed before the file name to prevent characters in the file name string from being treated as special characters. For example, if there is temp in the file address, t is treated as the tab character and the invalid address error is generated. The r makes the string raw, that is, it indicates that the string has no special characters. The r can be ignored if the file is in the same directory and the address is not being placed.

Here, file1 is created as an object for

MyFile1 and file2 as an object for MyFile2

.

Closing a

file

closes the file and frees up the memory space acquired by that file. It is used at the time when the file is no longer needed or if it is to be opened in a different file mode.

Syntax:

File_object.close()Read from a file

There are three ways to read data from a text file.

  • read() : Returns the read bytes as a string. Reads n bytes, if n is not specified, reads the entire file.

File_object.read([n])

  • readline() – Reads a line from the file and returns as a string. For n specified, reads at most n bytes. However, it does not read more than one line, even if n exceeds the length of the line.

File_object.readline([n])

  • readlines() : Reads all lines and returns them as each line a string element in a list.

File_object.readlines()Note: ‘n’ is

treated as a two-byte special character.

Example

:

Output:

The output of the read function is Hello This is Delhi This is Paris This is London The output of the Readline function is Hello The output of the Read(9) function is Hello The output of the Readline(9) function is Hello The output of the Readlines function is [‘Hello n’, ‘This is Delhi n’, ‘This is Paris n’, ‘This is London n’]

With the declaration

with the instruction in Python is used in exception handling to make the code cleaner and much more readable. Simplifies the management of common resources such as file flows. Unlike previous implementations, you do not need to call file.close() when you use the with statement. The declaration itself ensures the proper acquisition and release of resources.

Syntax

:with file name opened as file:

Output

:Hello This is Delhi This is Paris This is London

Note: To learn more about with the statement click here.

Contact US