How to read a text file in Python

0
65

Before reading a file, you should familiarize yourself with the world of files. You want to know what a computer software sees when dealing with files. It makes it easy for you to comprehend and construct file-reading logic.

A file is essentially a collection of bytes used to store data. This byte data is grouped into a file using a specific format. A file might be basic, like a text file, or more complicated, such as an executable software file. Whatever form of file you have, it will eventually be converted to 0s and 1s so that a computer can interpret it.

A file is divided into three sections:

  • Header- The header contains information about the file’s name, size, and type.
  • The contents of the file’s data.
  • End of Document (EOF) – a hidden character that draws attention to the file’s end.

We’ll only be working with.txt files in this article. However, keep in mind that there are tens of thousands of different file extensions.

Paths to Files

You must specify the path to a file on your system whenever you try to access it. As a result, simply calling the file by its name is insufficient. A file path is simply a string that points to the file’s location. The file path is broken down into three sections:

  • The folder’s location is the folder’s location in the file system. Backslashes (/) or forward slashes () are used to divide subsequent folders, depending on your system.
  • The filename is the file’s name.
  • The extension is the “.” at the end of a file extension, such as.txt or.jpeg. It specifies the file type in question.

A file path might look something like this:

When writing a Python program to read a file, you must know the file’s path. Then you must instruct the program to read the file’s contents until it reaches the EOF character. It may appear to be more complicated than it is. Python handles in the background a lot of the heavy lifting for you. As you’ll see, this allows you to read a complete file with just two lines of code.

Reading a Text File in Python

Python has built-in file creation, writing, and reading capabilities. In Python, two sorts of files can be handled: text files and binary files (written in binary language, 0s, and 1s). You will learn how to read text files in Python in this lesson.

  • Text files -Each line of text in this sort of file is terminated with a special character called EOL (End of Line), which in Python is the new line character (‘ n’).
  • Binary files – There is no terminator for a line in a binary file, and the data is stored after being converted into machine-readable binary language.

Modes of File Access

The kind of operations done on the already opened file are determined by the access modes. It specifies how the file is utilized after it has been opened. These modes additionally specify where the FileHandle is in the file. A filehandle acts as a cursor, indicating where data should be read or written in the file. Python has six different access modes.

Read Only (‘R’)

Open a text file for reading only (‘r’). The handle is at the beginning of the document. If the file does not exist, an I / O error is raised. It is also the default mode for opening files.

‘r +’ stands for Reading and Write

To read and write, open the file. The handle is at the beginning of the document. If the file does not exist, an I / O error is raised.

Only Write (” ‘)

To begin writing, open the file. The data in an existing file is truncated and overwritten. The handle is at the beginning of the document. If the file does not exist, it is created.

(‘w +’) Write and Read

To read and write, open the file. Data is shortened and overwritten for existing files. The handle is at the beginning of the document.

Only Append (‘a’)

To begin writing, open the file. If the file does not exist, it is created. The handle is at the end of the document. The data that is being written will be added after the existing data.

(‘a +’) Append and Read

To read and write, open the file. If the file does not exist, it is created. The handle is at the end of the document. The data that is being written will be added after the existing data. The following example demonstrates how to read all of the contents of the readme.txt file into a string:

with open('code.txt') as f:
    code_lines = f.readlines()

Python steps for reading a text file

Follow these steps to read a text file in Python:

  • To begin, use the open () function to open a text file for reading.
  • Second, use the file read (), readline (), or readlines () methods of the file object to read content from the text file.
  • Finally, use the file close () method to close the file.

1) The open () method

You’ll be focused on the first two parameters of the open () function.

The path_to_file option specifies the path to the text file.

Alternatively, use:

code_file_object = open(r"code.txt","Access_Mode")

The r is added before the filename to prevent special characters from being regarded as part of the filename string. If the file address contains the character temp, the character t is considered the tab character, and an invalid address error is raised. The r indicates that the string is raw, meaning that it contains no special characters. If the file is in the same directory as the address, the r is disregarded.

You only need to mention the file’s name if it’s in the same folder as the program. Otherwise, the file’s path is specified. You utilize the forward-slash (‘/’) to determine the file path when working in Windows.

For example, if the file is readme.txt and is located in the same sample folder as the application, the path to the file should be ‘home / tuts / code.txt.’ The mode argument is optional. It’s a string that indicates which mode you wish to open the file in. The following lists the several ways to open a text file:

  • ‘r’ – To read text, open a text file.
  • ‘w’ -To write text, open a text file.
  • ‘a’ -To append text, open a text file.

To open a file named codeunderscored.txt in the same folder as the application.open () is a built-in function. The open () method returns a file object with a read () method for accessing the file’s content. for example, use the following code:

f_code = open('code.txt','r')

If the file is at a different location, you must specify the file path, which looks like this:

f_code = open('/home/tuts/code.txt','r')

or

file_code = open(r"home/tuts/code.txt","w+")

You’ll use the open () function to read text from a text file.

2) Text-reading techniques

For reading text from a text file, the file object has three methods:

  • read () – reads the entire contents of a file into a string. This method is effective if you have a small file and wish to manipulate text as a whole.
  • readline () – reads each text file line and returns all the lines as strings.
  • readlines () – is responsible for returning a list of strings containing all of the lines in the text file.

You can read-only parts of the file Should. For instance, the read () method returns the entire text by default, but you can specify how many characters to return:

f_code = open("code.txt", "r")
print(f_code.read(3))

The example above returns the file’s first three characters.

The readline () method can return a single line. For example, read the following line from the file:

f_code = open("code.txt", "r")
print(f_code.readline())

You can read the first two lines by executing readline () twice. For example, the following code snippet reads the following two lines from the file:

f_code = open("code.txt", "r")
print(f_code.readline())
print(f_code.readline())

You may read the entire file line by line by looping through the lines of the file. For instance, the code below loops through the file line by line.

f_code = open("code.txt", "r")
for val in f_code:
  print(val)

3) Creating a file

A file is written in two ways.

write (): Writes the string str_one to the text file in a single line.

code_file_object.write(str_one)

writelines () inserts each string into a text file from a list of string elements. It is used to insert numerous lines at once.

code_file_object.writelines(L) for L = [str_one, str_two, str_three]

4) The procedure close ()

The file you open will remain open until you use the close () method to close it. It is critical to delete any files that are no longer in use. The software may crash, or the file may become corrupted if you do not close it. In addition, you should always close your files because changes to a file may not show up until you close it due to buffering. The following example explains how to close a file using the close () method:

You can use the with statement to automatically close the file without executing the close () method:

with open(path_to_file) as f:
    contents = f.readlines()

In practice, you’ll use the statement to close the file automatically.

f_code = open("code.txt", "r")
print(f_code.readline())
f_code.close()

Examples of reading a text file

For the demonstration, we’ll use the file code.txt. The read () method is used to read all of the contents of the code.txt file into a string in the following example:

with open('code.txt') as f:
    text_contents = f.read()
    print(text_contents)

The readlines () method is used to read a text file and return the contents as a list of strings in the following example:

code_lines = []
with open('code.txt') as f:
    code_lines = f.readlines()

count = 0
for ln in code_lines:
    count += 1
    print(f'ln count: ln')    

The following example demonstrates how to read a text file line by line with deadline ():

with open('code.txt') as f:
    code_line = f.readline()
    while code_line:
        code_line = f.readline()
        print(code_line)

A more efficient method of reading a text file line by line

The file object returned by the open () function is iterable. As a result, you may iterate over the lines of a text file using a for loop as follows:

with open('codeunderscored.txt') as f:
    for code_line in f:
        print(code_line)

It is a faster method of reading a text file line by line.

Reading Text files in UTF-8

With ASCII text files, the code in the preceding examples works perfectly. The text file is not a basic ASCII text file if you deal with different languages ​​like Japanese, Chinese, or Korean. It’s also most likely a UTF-8 file that contains more than just ASCII text characters.

To open a UTF-8 text file, use the encoding = ‘utf-8 ′ parameter to tell the open () method to anticipate UTF-8 characters from the file. For this tutorial, you’ll use the korean.txt file, which contains some Korean quotes. To loop over the korean_quotes.txt file, do the following:

with open('korean_quotes.txt', encoding='utf8') as f:
    for code_line in f:
        print(code_line.strip())

What is Python’s readable () method?

The readable () method can be used to determine whether or not a file can be read. True or False will be returned. Because we’re in reading mode, this example will return True:

file_code = open("code.txt")
print(file_code.readable())

If we switched to “w” (write) mode in this example, the readable () method would return False:

file_code = open("code.txt", "w")
print(file_code.readable())

Example: Program for demonstrating various ways to read and write data in a file

file_code = open("code.txt","w")
L = ["This is Code n","This is Underscored n","This is CodeUnderscored n"]

# n indicates the EOL (End of Line)
file_code.write("Codeunderscored Greetings! n")
file_code.writelines(L)
file_code.close() #to change the file access modes

file_code = open("code.txt","r+")

print("The Read function's output is ")
print(file_code.read())
print()

# seek(n) returns the nth file handle.
# beginning with a bite
file_code.seek(0)

print( " The Readline function's output is ")
print(file_code.readline())
print()

file_code.seek(0)

# To demonstrate the distinction between read and readline
print("The Readline(9) function returns  ")
print(file_code.read(9))
print()

file_code.seek(0)

print("The Readline(9) function returns ")
print(file_code.readline(9))

file_code .seek(0)
# readlines function
print("Readlines function output is: ")
print(file_code.readlines())
print()
file_code.close()

Example: Program illustrating appending vs. write mode

file_code = open("code.txt","w")
text_list = ["This is Code n","This is Underscored n","This is CodeUnderscored n"]
file_code.writelines(text_list)
file_code.close()

# append is responsible for adding at the end
file_code = open("code.txt","a")#append mode
file_code.write("Today n")
file_code.close()

file_code = open("code.txt","r")
print("Readlines output after appending  ")
print(file_code.readlines())
print()
file_code.close()

# Write-Overwrites
file_code = open("code.txt","w")#write mode
file_code.write("Tomorrow n")
file_code.close()

file_code = open("code.txt","r")
print("Readlines output after writing  ")
print(file_code.readlines())
print()
file_code.close()

Example: Opening, and reading file contents

def code_main():
     file_code= open("code.txt","w+")
     #file_code=open("code.txt","a+")

     for i in range(10):
         file_code.write("This is the code line %drn" % (i+1))
     file_code.close()   

     #Open the file back and read the contents
     file_code=open("code.txt", "r")
     if file_code.mode == 'r':
          contents =file_code.read()
          # printing the contents in the file
          #or, readlines reads the individual line into a list
          print(contents)
     file_code =file_code.readlines()
     for val in file_code:
        print(val)
if __name__== "__main__":
  code_main()

Summary

Use the open () method with the ‘r’ mode to open a text file for reading. To read a text file, use the read (), readline (), or readlines () methods. Always use the close () technique or the ‘with’ statement to close a file after reading it.

The close () function terminates a file and releases the memory space it has taken up. It’s utilized when the file is not needed anymore or needs to be accessed in a different file mode. On the other hand, the read () method will read the entire contents of the file as a single string. If the text file does not have a lot of content, this is a decent strategy to employ. It is critical to close a file once you have finished reading it using the close () function. It can be problematic if you fail to close your file. The keyword is one approach to ensure that your file is closed. It is considered best practice because the file will close automatically rather than requiring you to close it manually. To read a UTF-8 text file, use the encoding = ‘utf-8’ option.

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here