Monday, April 21, 2025

How to Read File - Python File Open

#Read all lines from file in IDLE

file = open('bulk-domains.txt', 'r')

res = file.read()

print(res)

 This Python script reads all the lines from a file named bulk-domains.txt in the current directory and prints the contents of the file to the console. Here is a line-by-line explanation:

#Read all lines from file in IDLE

This line is a comment that explains the purpose of the script.

file = open('bulk-domains.txt', 'r')

This line opens the file named bulk-domains.txt in read mode ('r') using the open() function and returns a file object. The file object is assigned to the variable file.

res = file.read()

This line reads the entire contents of the file object into a string variable called res using the read() method.

print(res)

This line prints the contents of the res string variable to the console using the print() function.

This Python script reads the contents of a file named bulk-domains.txt into a string variable and then prints the contents of the string to the console.

Script variant

#Read only 3 lines from file

file = open('bulk-domains.txt', 'r')

line_1 = file.readline()
line_2 = file.readline()
line_3 = file.readline()

print(line_1)
print(line_2)
print(line_3)

This Python script reads the first three lines of a file named bulk-domains.txt and prints them to the console. Here is a line-by-line explanation:

file = open('bulk-domains.txt', 'r')

This line opens the file named bulk-domains.txt in read mode ('r') using the open() function and returns a file object. The file object is assigned to the variable file.

line_1 = file.readline()
line_2 = file.readline()
line_3 = file.readline()

These lines read the first three lines of the file object into three separate string variables called line_1, line_2, and line_3, respectively, using the readline() method.

print(line_1)
print(line_2)
print(line_3)

These lines print the contents of the line_1, line_2, and line_3 string variables to the console using the print() function.

In summary, this Python script reads the first three lines of a file named bulk-domains.txt and prints them to the console. If the file has fewer than three lines, the corresponding variables will be empty strings, and they will be printed as empty lines to the console.

We can use simple for loop here


#Read 3 lines from file with for loop

for x in range(3):
    print(file.readline())

This Python code reads the first three lines of a file and prints them to the console. Here is a line-by-line explanation:

for x in range(3):

This line starts a for loop that will iterate three times, using the range() function to generate the sequence of numbers 0, 1, 2.

    print(file.readline())

This line is indented by four spaces, indicating that it is part of the loop. It does the same thing as the previous line: reads a line from the file and prints it to the console. Because it is inside the loop, it will be executed three times, once for each iteration of the loop.

The combination of these two lines within the loop reads the first three lines of the file and prints them to the console. If the file has fewer than three lines, it will only print the lines that are present in the file.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...