fileObject = open('thefile.txt') # if the file exists.
# do nothing
# close the file
fileObject.close()
It is same to the following.
withopen('thefile.txt') as fileObject:
# do nothing
pass
Basic operation
read file
# open file and create a file object
fileObject = open('thefile.txt') # if the file exists.
##########
# read all the content of the file
allTheContent = fileObject.read()
# read some bytes
someBytes = fileObject.read(100)
##########
# read a line to a string
aLine = fileObject.readline()
# read all lines to a list of strings
list_of_allTheLines = fileObject.readlines()
##########
# after using, close the opened file
fileObject.close()
write file
# open file and create a file object
fileObject = open('thefile.txt','w')
# it could be in another mode.
##########
# write some string
fileObject.write('I am the string.\n')
##########
# write a list of strings
fileObject.writelines(list_of_strings)
# another method
for stringg in list_of_strings:
fileObject.write(stringg)
# fileObject.writelines(stringg) seems to be same.
##########
# after using, close the opened file
fileObject.close()
Open mode
The function open is defined as def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True). Here are some documents about the mode.
Character
Meaning
‘r’
open for reading (default)
‘w’
open for writing, truncating the file first
‘x’
create a new file and open it for writing
‘a’
open for writing, appending to the end of the file if it exists
‘b’
binary mode
‘t’
text mode (default)
‘+’
open a disk file for updating (reading and writing)
‘U’
universal newline mode (deprecated)
from the offical document.
Another table:
mode
description
r
Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
rb
Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
r+
Opens a file for both reading and writing. The file pointer will be at the beginning of the file.
rb+
Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.
w
Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wb
Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
w+
Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
wb+
Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
a
Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
ab
Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
a+
Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
ab+
Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具