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
.
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. |
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