Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python - File Methods (Complete Guide for Beginners)

 In Python, file handling allows you to work with files for storing and managing data. Python provides several file methods that help you read, write, update, and manage files efficiently.

These methods are used after opening a file using open().

In this tutorial, you will learn all important Python file methods with examples.


What are File Methods?

File methods are built-in functions that allow you to:

  • Read file content
  • Write data to files
  • Move file cursor
  • Close files safely
  • Check file status

Opening a File

Before using file methods, you must open a file.

file = open("example.txt", "r")

1. read() Method

Reads the entire file content.

Example:

file = open("example.txt", "r")

content = file.read()

print(content)

file.close()

2. readline() Method

Reads one line at a time.

Example:

file = open("example.txt", "r")

print(file.readline())
print(file.readline())

file.close()

3. readlines() Method

Reads all lines and returns a list.

Example:

file = open("example.txt", "r")

lines = file.readlines()

print(lines)

file.close()

4. write() Method

Writes a string into a file.

Example:

file = open("example.txt", "w")

file.write("Hello Python File Methods")

file.close()

5. writelines() Method

Writes multiple lines into a file.

Example:

file = open("example.txt", "w")

lines = ["Line 1\n", "Line 2\n", "Line 3\n"]

file.writelines(lines)

file.close()

6. close() Method

Closes the file after operations.

Example:

file = open("example.txt", "r")

print(file.read())

file.close()

Why important?

  • Frees system resources
  • Prevents data corruption

7. seek() Method

Moves the file pointer to a specific position.

Example:

file = open("example.txt", "r")

file.seek(0)

print(file.read())

file.close()

8. tell() Method

Returns the current position of the file pointer.

Example:

file = open("example.txt", "r")

print(file.tell())

file.close()

9. flush() Method

Forces writing of buffered data to the file.

Example:

file = open("example.txt", "w")

file.write("Hello")
file.flush()

file.close()

10. Using with Statement (Best Practice)

Automatically handles file closing.

Example:

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

Real-World Example: Logging System

with open("log.txt", "a") as file:
    file.write("User logged in\n")

Real-World Example: Student Record System

with open("students.txt", "w") as file:
    file.write("Name: John\nMarks: 90")

File Methods Summary

MethodDescription
read()Read full file
readline()Read one line
readlines()Read all lines
write()Write text
writelines()Write multiple lines
close()Close file
seek()Move cursor
tell()Get cursor position
flush()Save buffer
with open()Safe handling

Common Mistakes

Mistake 1: Forgetting to close file

❌ Wrong:

file = open("example.txt", "r")
print(file.read())

✔ Always use close() or with


Mistake 2: Using wrong mode

open("file.txt", "r").write("Hello")

✔ Read mode cannot write


Mistake 3: Overwriting data accidentally

open("file.txt", "w")

✔ Deletes old data


Safe File Method Example

with open("data.txt", "a") as file:
    file.write("Safe write operation\n")

Conclusion

Python file methods are essential for working with file data efficiently.

You learned:

  • How to read and write files
  • File pointer control methods
  • Safe file handling techniques
  • Real-world examples

Mastering file methods helps you build powerful applications like data storage systems, logs, and automation tools.




Post a Comment

0 Comments