Python file operations


After using open to open a file, you must remember to call the close() method of the file object. For example, you can use the try/finally statement to ensure that the file can be closed finally.

file_object = open('thefile.txt')
try:
     all_the_text = file_object.read( )
finally:
     file_object.close( )

Note: The open statement cannot be placed in the try block, because when an exception occurs when opening the file, the file object file_object cannot execute the close() method.


Click (here) to collapse or open

#coding:utf-8
#!/usr/bin/python
# Filename: fileRW.py
import os

context=""" hello world,hello
"r" 以读方式打开,只能读文件 , 如果文件不存在,会发生异常 
"w" 以写方式打开,只能写文件, 如果文件不存在,创建该文件
                                                     如果文件已存在,先清空,再打开文件"""
def createfileBYwrite(filename):
    f = open(filename,'w') #打开文件open()是file()的别名
    f.write(context) #把字符串写入文件
    f.close() #关闭文件

#注意,调用writelines写入多行在性能上会比使用write一次性写入要高。
def createfileBYwritelines(filename):
    f = open(filename,'w') #打开文件open()是file()的别名
    try:
        f.writelines(context) #把字符串写入文件
    finally: 
        f.close() #关闭文件

def readBYreadline(filename):
    f = open(filename)
    while True:
        line =f.readline()#当文件指针移动到末尾继续readline会出现错误,所以后面要加判断
        if line:
            print line,
        else:
            break
    f.close()

#需要通过循环访问readlines()返回列表中的元素
def readBYreadlines(filename):
    f = open(filename)
    try:
        lines = f.readlines()
        for line in lines:
            print line,
    finally:
        f.close()
    
#从文件中读取所有内容,赋予一个字符串变量
def readBYread(filename):
    f = open(filename)
    try:
        content = f.read()
    finally:
        f.close()
    print content 
    
if __name__== "__main__":
    filename="hello.txt"
    createfileBYwritelines(filename)
    readBYread(filename)
    #把文件删除掉
    if os.path.exists(filename):
        print "文件存在%s" %filename
        os.remove(filename)
    
    
 hello world,hello
"r" 以读方式打开,只能读文件 , 如果文件不存在,会发生异常 
"w" 以写方式打开,只能写文件, 如果文件不存在,创建该文件
                                                     如果文件已存在,先清空,再打开文件
文件存在hello.txt


2. Read files

Read text files

input = open('data', 'r')
#第二个参数默认为r
input = open('data')

Read binary files

input = open('data', 'rb')

Read all contents

file_object = open('thefile.txt')
try:
     all_the_text = file_object.read( )
finally:
     file_object.close( )

Read fixed bytes

file_object = open('abinfile', 'rb')
try:
    while True:
         chunk = file_object.read(100)
        if not chunk:
            break
         do_something_with(chunk)
finally:
     file_object.close( )

Read each line

list_of_all_the_lines = file_object.readlines( )


If the file is a text file, you can also directly traverse the file object to get each line:

for line in file_object:
     process line

3. Write file

Write text file

output = open('data', 'w')

Write binary file

output = open('data', 'wb')

Append writing file

output = open('data', 'w+')

Write data

file_object = open('thefile.txt', 'w')
file_object.write(all_the_text)
file_object.close( )

Write multiple lines

file_object.writelines(list_of_text_strings)


Note that calling writelines to write multiple lines has better performance than using write to write in one go Be high.

When processing log files, we often encounter such a situation: the log file is huge and it is impossible to read the entire file into the memory for processing at one time. For example, it needs to be processed on a computer with a physical memory of 2GB. Processing a 2GB log file on the machine, we may want to process only 200MB of its content at a time.
In Python, the built-in File object directly provides a readlines(sizehint) function to accomplish such a thing. Take the following code as an example:

file = open('test.log', 'r') sizehint = 209715200   # 200M position = 0 lines = file.readlines(sizehint) while not file.tell() - position < 0:        position = file.tell()        lines = file.readlines(sizehint)


Every time the readlines(sizehint) function is called, approximately 200MB of data will be returned, and the returned data must be complete row data. In most cases, the number of bytes of the returned data will be slightly smaller than sizehint. The specified value is larger (except when the readlines(sizehint) function is called for the last time). Normally, Python will automatically adjust the user-specified sizehint value to an integer multiple of the internal cache size.


1. Use Python to create a new file. The content is an integer from 0 to 9. Each number occupies one line:

#python
>>>f=open('f.txt','w')    # r只读,w可写,a追加
>>>for i in range(0,10):f.write(str(i)+'\n')
.  .  .
>>> f.close()


2. Append file content, 10 random integers from 0 to 9:
#python
>>>import random
>>>f=open('f.txt','a')
>>>for i in range(0,10):f.write(str(random.randint(0,9)))
.  .  .
>>>f.write('\n')
>>>f.close()


3. Append file content, random integers from 0 to 9, 10 numbers per line, 10 lines in total :
#python
>>> import random
>>> f=open('f.txt','a')
>>> for i in range(0,10):
.  .  .     for i in range(0,10):f.write(str(random.randint(0,9))) 
.  .  .     f.write('\n')    
.  .  .
>>> f.close()


4. Direct the standard output to the file:
#python
>>> import sys
>>> sys.stdout = open("stdout.txt", "w")
>>>  . . .


5. Read and write files

1. File opening:

f = file(name[, mode[, buffering]])

Entry parameters: name file name

mode option, string

buffering Whether to buffer (0=no buffering, 1=buffering, >1 int number = buffer size)

Return value: File object

mode option:

"r" Open in read mode, only the file can be read. If the file does not exist, an exception will occur.

"w" Open in a way of writing, you can only write files. If the file does not exist, create this file

If the file already exists, clear the file first, then open the file

"Rb" The file can only be read. If the file does not exist, an exception will occur.

"wb" is opened in binary writing mode. The file can only be written. If the file does not exist, create the file.

If the file already exists, the file will be created. Exists, clear it first, and then open the file

"rt" Open in text reading mode, you can only read the file. If the file does not exist, an exception will occur

"wt" Open in text writing mode , can only write files, if the file does not exist, create the file

If the file exists, clear the file first, and then open the file

# "RB" to open it in a binary reading. You can read and write files. wb " Open in binary writing mode, you can read and write the file. If the file does not exist, create the file

 Close the file

f.close()

After the file is read and written, the file should be closed.

3. Clear the file content

f.truncate()

Note: Only when starting with "r " "rb " "w" "wb" "wb "Only files opened in writable mode can perform this function.

4. File pointer positioning and query

(1) File pointer:

After the file is opened, its object is saved in f. It will remember the current position of the file in order to perform read and write operations. This position is called the file pointer (a long type number of bytes calculated from the beginning of the file).

(2) The position when the file is opened:

Files opened with "r" "r " "rb " reading mode, "w" "w " "wb " writing mode,

Initially, the file pointers point to the head of the file.

(3) Get the value of the file pointer:

L = f.tell()

(4) Move the file pointer

    f.seek(  offset, option)

   When option=0, it means that the file pointer points from the file head to the "offset" byte.

When option =1, it means to point the file pointer to the current position of the file and move backward by "offset" bytes.

When option =2, it means to point the file pointer to the end of the file and move the "offset" bytes forward.

5. Reading the content from the file

1 Reading of text files (files opened in "rt" mode)

s = f.readline( )

Return value: s is a string, a line read from the file, including the line terminator.

Explanation: (1) If len( s ) =0, it means the end of the file has been reached

(2) If it is the last line of the file, there may be no line terminator

2 Reading of binary files (files opened in "rb", "rb ", "wb " mode)

s = f.read( n )

For pictures, videos and other files, you must use the b mode to read and write

Notes: (1) If len( s ) =0, it means the end of the file has been reached

2) After the file is read, the file pointer moves backward by len(s) bytes.

                    (3) If the track is damaged, an exception will occur.

6. Write a string to the file

f.write( s )

Parameters: s to be written Input string

Description: (1) After the file is written, the file pointer moves backward by len(s) bytes.

                  (2) If the track is damaged or the disk is full, an exception will occur.

      

Return value: s is a string, the content read from the file

7. Delete the file

import os

os.remove(file)