Nine, file operations

First, open the file: open file or can be, it can only open up after 3.0
Open the file in read-only mode:
file_obj = open("E:\log.txt","r")
Open a file for reading and writing:
file_obj = open ( "E: \ log.txt", "r +") ====> readable, writable, appendable
Open the file in append mode:
file_obj = open ( "E: \ log.txt", "a +") ====> readable, does not exist is created, only the presence of the additional content
 
 
r, read-only mode (default).
w, write-only mode. [Unreadable; it does not exist, create; there is deleting content;]
a, append mode. [Readable; it does not exist, create; there is only the additional content;]
"+" Indicates a file can read and write at the same time
 
r +, can read and write files. [Read; write; appendable]
w +, the same w
a +, the same a
Note: Only the + r + meaningful, and the other exactly the same with or without the +
Note: r + should be used with caution, because r + default overwrites the contents inside, began to cover from the first row, so if you want to append content is best to use a +
 
"U" indicates the time of reading can be r \ r \ n-automatically converted into \ \ n \ n (used only with r + r or mode)
 
rU
r+U
"B" represents the binary files (eg: for an FTP transmission marked when ISO file uploading, linux negligible, windows handle binary files)
 
rb
wb
from
 
==============================================================================
==============================================================================
==============================================================================
Second, file operations:
class file(object):
 
def close(self): # real signature unknown; restored from __doc__
Close the file
 
def fileno(self): # real signature unknown; restored from __doc__
文件描述符
"""
fileno() -> integer "file descriptor".
 
This is needed for lower-level file interfaces, such os.read().
"""
return 0
 
def flush(self): # real signature unknown; restored from __doc__
刷新文件内部缓冲区
""" flush() -> None. Flush the internal I/O buffer. """
pass
 
 
def isatty(self): # real signature unknown; restored from __doc__
判断文件是否是同意tty设备
 
def next(self): # real signature unknown; restored from __doc__
获取下一行数据,不存在,则报错
 
def read(self, size=None): # real signature unknown; restored from __doc__
读取指定字节数据
"""
read([size]) -> read at most size bytes, returned as a string.
 
 
def readinto(self): # real signature unknown; restored from __doc__
读取到缓冲区,不要用,将被遗弃
 
def readline(self, size=None): # real signature unknown; restored from __doc__
仅读取一行数据
 
 
def readlines(self, size=None): # real signature unknown; restored from __doc__
读取所有数据,并根据换行保存值列表
 
 
def seek(self, offset, whence=None): # real signature unknown; restored from __doc__
指定文件中指针位置
 
 
def tell(self): # real signature unknown; restored from __doc__
获取当前指针位置
 
 
def truncate(self, size=None): # real signature unknown; restored from __doc__
截断数据,仅保留指定之前数据,即指针到了哪里,后面的数据会全部删掉。(这是不带参数的含义)
truncate(5)如果带了参数,则代表只保留前面5个字节,后面的都不要了。
 
def write(self, p_str): # real signature unknown; restored from __doc__
写内容
 
 
def writelines(self, sequence_of_strings): # real signature unknown; restored from __doc__
将一个字符串列表写入文件
 
 
for line in file_obj:
#每次循环,只读一行。避免全部读入内存
==============================================================================================
===============================================================================================
===============================================================================================
三、with
为了避免打开文件后忘记关闭,可以通过管理上下文,即:
 
with open('log','r') as obj:
obj.write('abcdefg')
.
.
.
如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。
 
在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:
 
with open('log1','r') as obj1, open('log2','w') as obj2:
for line in obj1:
new_line = line.replace('10.0.0.1','10.0.0.2')
obj2.write(new_line)
假设现在我有这样一个需求:我现在一个nginx服务器在线上运行着,我要修改它的配置文件,我如果直接去改,是不是怕出错,然后无法回滚
我可以这样做:nginx.conf以只读的方式打开,并且将其中的ip为10.0.0.1的字段改为10.0.0.2写入到nginx2.conf中,然后通过nginx的check
检查一下这个nginx2.conf有没有问题,如果没有问题我就用这个,改一下名为nginx.conf,将原来的改为nginx.conf.bak,这样做如果我这个
配置文件修改的有点问题,我也可以从nginx.conf.bak文件回滚到之前的状态。这个replace的参数是碰到10.0.0.1我就改成10.0.0.2,如果没
碰到的话,new_line = line.
=================================================================================================
=================================================================================================
=================================================================================================
四、那么问题来了.......
 

Guess you like

Origin www.cnblogs.com/steven9898/p/11329358.html