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 — run this first. Change values and press Run again.
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.
### 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.).
## Read a file line by line
with open('example.txt','r') as file:
for line in file:
print(line.strip()) ## sstrip() removes the newline characterBrowser practice only — full example needs Python on your computer (files, Flask, threads, etc.).
## 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.).
## 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.).
### 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.).
### 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.).
# 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.).
### 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.).
#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).
### 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.