file properties and methods

UpComing SoftWare Training Demos

πŸš€ *Gen AI EngineerΒ Telugu (Production Focused)*
πŸ—“οΈ *Date:* 30th Sept 2026, 07:00AM IST
πŸ“ *Register Now!:*
https://www.vlrt.in/gr
πŸ‘₯ *Join WA Community:*
https://www.vlrt.in/gw
πŸ’‘*Course Content:*
https://www.vlrt.in/ai
▢️ *Demo Videos:*
https://www.vlrt.in/gv

πŸš€ *Service now Admin/ Development (ITSM)FREE Demo in Telugu*
πŸ—“οΈ *Date:* 30th Sept 2026, 08:00AM IST
πŸ“ *Register Now!:*
https://www.vlrt.in/7r
πŸ‘₯ *Join WA Community:*
https://www.vlrt.in/7w
πŸ’‘*Course Content:*
https://www.vlrt.in/7c
▢️ *Demo Videos:*
https://www.vlrt.in/7v

πŸš€ *Vulnerability Management Training Demo*
πŸ—“οΈ *Date:* 30th Sept 2026, 09:00 AM IST
πŸ“*Register Now!:*
https://www.vlrt.in/vr
πŸ‘₯ *Join Community:*
https://www.vlrt.in/cw
πŸ’‘ *Course Content:*
https://www.vlrt.in/vc
▢️ *Demo Videos:*
https://www.vlrt.in/Vm

1. file.closed – Is the file door shut?

Think of a file like a door. file.closed tells you if the door is open or closed.

python

# Open the file (open the door)
f = open("test.txt", "w")
f.write("Hello!")

print(f.closed)  # Output: False (door is open)

# Close the file (close the door)
f.close()

print(f.closed)  # Output: True (door is closed)

2. file.mode – What can you do with the file?

This tells you how the file was opened – for reading, writing, or both.

python

# Open for reading
f1 = open("test.txt", "r")
print(f1.mode)  # Output: r (read only)
f1.close()

# Open for writing  
f2 = open("test.txt", "w")
print(f2.mode)  # Output: w (write only)
f2.close()

# Open for reading and writing
f3 = open("test.txt", "r+")
print(f3.mode)  # Output: r+ (read and write)
f3.close()

3. file.name – What’s the file’s name?

This simply tells you the name of the file you’re working with.

python

# Open a file
f = open("my_data.txt", "w")

print(f.name)  # Output: my_data.txt

f.write("Some data")
f.close()

Simple Example Showing All Three:

python

# Create and work with a file
file = open("diary.txt", "w")  # Open for writing

print("File name:", file.name)    # diary.txt
print("File mode:", file.mode)    # w
print("Is closed?", file.closed)  # False

file.write("Today was a good day.")
file.close()

print("Is closed now?", file.closed)  # True

The Easiest Way: Using with

The best practice is to use with – it automatically closes the file for you!

python

with open("story.txt", "w") as file:
    file.write("Once upon a time...")
    print("Mode:", file.mode)   # w
    print("Name:", file.name)   # story.txt
    print("Closed?", file.closed)  # False (still open inside the block)

# Outside the block, the file is automatically closed
print("Closed now?", file.closed)  # True

Simple Summary:

  • .closedΒ β†’ Is the file open or closed? (True/False)
  • .modeΒ β†’ How was it opened? (r/w/a)
  • .nameΒ β†’ What’s the file called?

These are just simple properties of the file object you’re working with!

1. file.close()

Closes the file to free up resources.

python

# Manual way (easy to forget to close!)
f = open("test.txt", "w")
f.write("Hello World")
f.close()  # Don't forget this!

# Better way using 'with' (automatically closes)
with open("test.txt", "w") as f:
    f.write("Hello World")
# File automatically closes here

2. file.read(size)

Reads content from the file.

python

with open("test.txt", "w") as f:
    f.write("Hello World, this is Python!")

with open("test.txt", "r") as f:
    print(f.read(5))   # Reads first 5 characters: "Hello"
    print(f.read(6))   # Reads next 6 characters: " World"
    print(f.read())    # Reads the rest: ", this is Python!"

3. file.readline()

Reads one line at a time.

python

with open("story.txt", "w") as f:
    f.write("Line 1\nLine 2\nLine 3")

with open("story.txt", "r") as f:
    print(f.readline())  # "Line 1\n"
    print(f.readline())  # "Line 2\n"
    print(f.readline())  # "Line 3"

4. file.readlines()

Reads all lines into a list.

python

with open("story.txt", "w") as f:
    f.write("First line\nSecond line\nThird line")

with open("story.txt", "r") as f:
    lines = f.readlines()
    print(lines)  # ['First line\n', 'Second line\n', 'Third line']

5. file.write(string)

Writes text to the file.

python

with open("output.txt", "w") as f:
    chars_written = f.write("Hello Python!")
    print(f"Wrote {chars_written} characters")  # Wrote 13 characters

6. file.writelines(list)

Writes a list of strings to the file.

python

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

with open("multi_lines.txt", "w") as f:
    f.writelines(lines_to_write)
# File contains: Line 1
#                Line 2
#                Line 3

7. file.seek(offset)

Moves the file pointer to a specific position.

python

with open("test.txt", "w") as f:
    f.write("0123456789")

with open("test.txt", "r") as f:
    print(f.read(3))  # "012"
    f.seek(0)         # Go back to start
    print(f.read(3))  # "012" again
    f.seek(5)         # Jump to position 5
    print(f.read(3))  # "567"

8. file.tell()

Tells you the current position in the file.

python

with open("test.txt", "w") as f:
    f.write("Hello World")

with open("test.txt", "r") as f:
    print(f.tell())  # 0 (start)
    f.read(5)
    print(f.tell())  # 5 (after reading 5 characters)
    f.read(3)
    print(f.tell())  # 8 (after reading 3 more)

Complete Example Showing All Methods:

python

# Create a file
with open("example.txt", "w") as f:
    f.write("Line One\nLine Two\nLine Three")

# Read and manipulate the file
with open("example.txt", "r+") as f:  # r+ = read and write
    print("Initial position:", f.tell())  # 0
    
    # Read first line
    first_line = f.readline()
    print("First line:", first_line.strip())
    print("Position after readline:", f.tell())  # 9
    
    # Jump to beginning
    f.seek(0)
    print("Position after seek(0):", f.tell())  # 0
    
    # Read all content
    content = f.read()
    print("Full content:", content)
    
    # Go to end and add new text
    f.seek(0, 2)  # 2 = end of file
    f.write("\nLine Four")
    
    # Check final position
    print("Final position:", f.tell())

Simple Summary:

  • close()Β – Shut the file when done
  • read()Β – Read content from file
  • readline()Β – Read one line
  • readlines()Β – Read all lines as a list
  • write()Β – Write text to file
  • writelines()Β – Write a list of lines
  • seek()Β – Move to a specific position
  • tell()Β – Check current position

Always use with statement – it automatically handles closing the file for you!

Similar Posts

  • Case Conversion Methods in Python

    Case Conversion Methods in Python (Syntax + Examples) Python provides several built-in string methods to convert text between different cases (uppercase, lowercase, title case, etc.). Below are the key methods with syntax and examples: 1. upper() β€“ Convert to Uppercase Purpose: Converts all characters in a string to uppercase.Syntax: python string.upper() Examples: python text = “Hello, World!”…

  • Number ManipulationΒ andΒ F-StringsΒ in Python, with examples:

    Python, mathematical operators are symbols that perform arithmetic operations on numerical values. Here’s a breakdown of the key operators: Basic Arithmetic Operators: Other Important Operators: Operator Precedence: Python follows the standard mathematical order of operations (often remembered by the acronym PEMDAS or BODMAS): Understanding these operators and their precedence is essential for performing calculations in…

  • positive lookbehind assertion

    A positive lookbehind assertion in Python’s re module is a zero-width assertion that checks if the pattern that precedes it is present, without including that pattern in the overall match. It’s the opposite of a lookahead. It is written as (?<=…). The key constraint for lookbehind assertions in Python is that the pattern inside the…

  • Unlock the Power of Python: What is Python, History, Uses, & 7 Amazing Applications

    What is Python and History of python, different sectors python used Python is one of the most popular programming languages worldwide, known for its versatility and beginner-friendliness . From web development to data science and machine learning, Python has become an indispensable tool for developers and tech professionals across various industries . This blog post…

Leave a Reply

Your email address will not be published. Required fields are marked *