Python File Processing

Get your hand dirty

title = 'aaa'
with open('%s.txt' % (title,),'a') as f:
f.write('---------**-----------\n')
for i in range(10):
f.write(str(i) + '\n')
f.write('%%%%%%%%%%%%%\n')

Basic basic operations

# open file and create a file object
fileObject = open('thefile.txt') # if the file exists.
# do nothing
# close the file
fileObject.close()

It is same to the following.

with open('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.

CharacterMeaning
‘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:

modedescription
rOpens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
rbOpens 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.
wOpens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wbOpens 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.
aOpens 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.
abOpens 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.

ref:
Confused by python file mode “w+” - Stack Overflow
https://stackoverflow.com/questions/16208206/confused-by-python-file-mode-w

Reference

Python读写文件 - AllenW - 博客园
https://www.cnblogs.com/allenblogs/archive/2010/09/13/1824842.html
Python文件操作之open()的mode - CSDN博客
http://blog.csdn.net/tyronne/article/details/46930647

posted on   yusisc  阅读(36)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· 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工具

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示