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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值