Language

File Handling · Lesson 19 of 56

Fileoperation

Source: 6-File Handling/6.1-fileoperation.ipynb

Start here — no coding background needed

What you will learn

Understand read/write files conceptually — full file demos need your computer.

In simple words

Programs can save text to a file and read it back. Paths and permissions matter on real disks.

Think of it like this

Saving a note in Notes app and opening it tomorrow.

Save and read notes in files — useful later; we keep examples tiny here.

Easy example — try this first

Easy example — run this first. Change values and press Run again.

Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference notes (from full bootcamp)

Optional — deeper detail for when you are ready

File Operation- Read And Write Files

File handling is a crucial part of any programming language. Python provides built-in functions and methods to read from and write to files, both text and binary. This lesson will cover the basics of file handling, including reading and writing text files and binary files.

Example HCL
HCL
### Read a Whole File

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

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Example HCL
HCL
## Read a file line by line
with open('example.txt','r') as file:
    for line in file:
        print(line.strip()) ## sstrip() removes the newline character

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Example HCL
HCL
## Writing a file(Overwriting)

with open('example.txt','w') as file:
    file.write('Hello World!\n')
    file.write('this is a new line.')

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Example HCL
HCL
## Write a file(wwithout Overwriting)
with open('example.txt','a') as file:
    file.write("Append operation taking place!\n")

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Example HCL
HCL
### Writing a list of lines to a file
lines=['First line \n','Second line \n','Third line\n']
with open('example.txt','a') as file:
    file.writelines(lines)

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Example HCL
HCL
### Binary Files

# Writing to a binary file
data = b'\x00\x01\x02\x03\x04'
with open('example.bin', 'wb') as file:
    file.write(data)

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Example HCL
HCL
# Reading a binary file
with open('example.bin', 'rb') as file:
    content = file.read()
    print(content)

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Example HCL
HCL
### Read the content froma  source text fiile and write to a destination text file
# Copying a text file
with open('example.txt', 'r') as source_file:
    content = source_file.read()

with open('destination.txt', 'w') as destination_file:
    destination_file.write(content)

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Example HCL
HCL
#Read a text file and count the number of lines, words, and characters.
# Counting lines, words, and characters in a text file
def count_text_file(file_path):
    with open(file_path, 'r') as file:
        lines = file.readlines()
        line_count = len(lines)
        word_count = sum(len(line.split()) for line in lines)
        char_count = sum(len(line) for line in lines)
    return line_count, word_count, char_count

file_path = 'example.txt'
lines, words, characters = count_text_file(file_path)
print(f'Lines: {lines}, Words: {words}, Characters: {characters}')

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

The w+ mode in Python is used to open a file for both reading and writing. If the file does not exist, it will be created. If the file exists, its content is truncated (i.e., the file is overwritten).

Example HCL
HCL
### Writing and then reading a file

with open('example.txt','w+') as file:
    file.write("Hello world\n")
    file.write("This is a new line \n")

    ## Move the file cursor to the beginning
    file.seek(0)

    ## Read the content of the file
    content=file.read()
    print(content)

Browser practice only — full example needs Python on your computer (files, Flask, threads, etc.).

Practice test — try yourself

Write code, press Check. Wrong answer shows the correct code to copy & run.

You learned "Fileoperation". Use print() to show: Done: Fileoperation

Hint: Use one print() with the exact text.

Python