File Operation Examples
This document introduces file reading/writing, positioning, and exception handling.
Module Introduction
Supports open(), read/readline/readlines, write/writelines, seek, close, etc. Built-in examples:
#!pika
f = open('data.txt', 'r')
s = f.read(10)
f.close()
#!pika
#!pika
f = open('data.txt', 'w')
f.write('Hello\n')
f.close()
#!pika
Example Code
Basic Read/Write (file.py)
#!pika
f = open('data.txt', 'r')
s = f.read(10)
print(s)
f.close()
f = open('data.txt', 'rb')
b = f.read(-1)
print(b)
f.close()
f = open('data2.txt', 'w')
f.write('Hello World!\n')
f.close()
#!pika
Note: Read first 10 bytes of text, read entire binary file, write text. Change paths to actually existing files on device.
Read by Lines (file2.py)
#!pika
f = open('data.py', 'r')
s = f.readline()
print(s)
lines = f.readlines()
print(lines)
f.close()
#!pika
Note: readline() reads one line, readlines() reads all remaining lines into list.
Multi-line Writing (file3.py)
#!pika
f = open('data3.txt', 'w')
seq = [
'This is the first line.\n',
'This is the second line.\n',
'This is the third line.\n'
]
f.writelines(seq)
f.close()
#!pika
Note: writelines() writes string list to file, doesn’t add newlines automatically, include \n in strings.
File Not Found Handling (file_nofound.py)
#!pika
try:
f = open("_no_file.txt", "r")
except:
print("File not found")
#!pika
Note: Use try/except to catch open failures, prevent script interruption due to missing files.
File Positioning (seek.py)
#!pika
f = open('data.txt', 'rb')
len = f.seek(0, 2)
print(len)
f.close()
#!pika
Note: seek(0, 2) moves position to end of file and returns current offset (i.e., file length). Mode 2 means calculate from end.
write Method Reference (write_fn.py)
#!pika
f = open('test.txt', 'w')
w = f.write
n = w('test')
f.close()
assert n == 4
print('PASS')
#!pika
Note: After assigning f.write to variable and calling, return value is number of bytes written.
Large File Writing and Memory (write_issue.py)
#!pika
from PikaStdLib import MemChecker as mem
a = open("test.py", "wb")
m0 = mem().getNow()
for i in range(1000):
n = a.write(b'# print(r_data[0])\n# print(r_data[1])\n\n# import _thread\n# import')
assert n == 64
m1 = mem().getNow()
for i in range(1000):
n = a.write(b'import osTimer33332 cnt=0332 cnt1=033332 def test_cb(arg):332global cnt332cnt+=1332123123123123231231')
assert n == 101
m2 = mem().getNow()
a.close()
print('PASS')
print('mem: ', m2- m0)
exit()
#!pika
Note: Loop writing and use MemChecker to observe memory changes; similar approach can be used for simple memory monitoring on device.
Large File Reading (bigfile.py)
#!pika
f = open('test.py', 'rb')
b = f.read(-1)
f.close()
#!pika
Note: read(-1) reads entire file into memory, watch device RAM limits for large files.
Notes
- Paths must conform to device filesystem (like LittleFS), and files must exist or directories must be writable.
- Should
close()after read/write, or usewith(if runtime supports) to avoid resource leaks. - Use
'rb'/'wb'for binary mode,'r'/'w'for text mode.