prev
next
Or press ESC to close.
To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content. To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content.

Python Programming Part 16

Rajjit Nov 02nd, 2023 15 mins read
part-16

TOPIC: Python File Functions and File Modes Uses

Python File Functions:
1. read()
2. readline()
3. readlines()
4. write()
5. writelines()
6. seek()
7. tell() etc.

Reading function:
Python offers three primary methods for reading files: read(), readline() and readlines(). These functions are used to access the contents of a file that has been opened for reading.

read() function:
The read() function reads the entire content of the file and returns it as a single string. It's suitable for situations where we need the entire file content as a single string. Example:

with open("example.txt", "r") as file:
    content = file.read()
    print(content)

readline() function:
The readline() function reads a single line from a file, allowing you to process files line by line. It returns each line as a string.
Example of using readline() to read lines from a File:

with open("example.txt", "r") as file:
    line1 = file.readline()
    line2 = file.readline()
    print("Line 1:", line1)
    print("Line 2:", line2)

readlines() function:
The readlines() function reads the file line by line and returns a list of strings, with each string representing a line. It's commonly used for processing text files with multiple lines of data.
Example of using readlines() to read lines from a File:

with open("example.txt", "r") as file:
    lines = file.readlines()
    for line in lines:
        print(line)

The read() function can also be used with an argument to specify the number of characters to read. For example, file.read(100) reads the next 100 characters from the file.
When using readlines(), we can iterate through the list of lines to process them individually, making it suitable for reading and processing multiple lines of data in a file.
read(), readline() and readlines() functions have their specific use cases, and the choice between them depends on whether we need the entire content as a single string or we want to process the file line by line.

write() function:
The write() function is used to write data to a file that has been opened in write mode ("w") or append mode ("a"). We can use write() to add text or data to a file. It's important to note that write() does not automatically add line breaks. If we want to start a new line, we need to include the newline character \n.
Example of using write() function:

with open("output.txt", "w") as file:
    file.write("Hello, World!\n")
    file.write("This is a new line.")

writelines() function:
The writelines() function is used to write multiple lines to a file. It expects a sequence of lines (a list of strings) as its argument.
Example of using writelines() functions:

lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
with open("output.txt", "w") as file:
    file.writelines(lines)

seek() function or file positioning function:
The seek() function allows us to change the current position within a file. We can use it to move the file pointer to a specific byte position.
Example for using seek() function:

with open("example.txt", "r") as file:
    file.seek(0)  # Move to the top byte in the file
    file.seek(10)  # Move to the 11th byte in the file
    file.seek(n)  # Move to the nth byte in the file
    content = file.read()
    print(content)

tell() function or current file position function:
The tell() function returns the current file position, which is the byte offset from the beginning of the file. It's useful for tracking the position in the file.
Example for using tell() function:

with open("example.txt", "r") as file:
    position = file.tell()  # Get the current position
    print("Current position:", position)

By using these python file functions, we will be able to perform any function to any file. For full explanation of the topics in this blog, check out the YouTube video in the above section. Thank you...

Code as discussed in the video:

# read(), readline(), readlines(), write(), writelines(), seek(), tell()
# python file commands

# a = open('tut15.txt', 'r') # read
# a = open('tut15.txt', 'a') # append
# a = open('tut15.txt', 'w') # write
# a = open('tut15.txt', 'x') # create
# a = open('tut15.txt', 'w+') # write
# a.write('This is the Python Basics for Beginners Course by RJ Coding Tips')# this will point at the end of the file
# a.seek(0)# without this, it will not show the contents - it will point to the top part of the content if 0
# content = a.read() 
# print(content)


# readline() - used to read a single line
# a = open('tut15.txt', 'r') 
# # content = a.readline()
# # readlines() - used to read all the lines
# content = a.readlines()
# print(content)
# a.close()

# writelines
names = ["Rajjit", "Tomba", "Chaoba", "Sigma"]
with open('tut15.txt', 'w') as a:
    a.writelines([name + "\n" for name in names])
a.close()

You can copy and run this code