python文件对象
文件系统和文件:
文件系统是OS用于明确磁盘或分区上的文件的方法和数据结构--即在磁盘上组织文件的方法
计算机文件(或称文件、电脑档案、档案)是存储在某种长期存储设备或临时存储设备中的一段数据流,并且归属于计算机文件系统管理之下
概括来讲:
文件是计算机中由OS管理的具有名字的存储区域
在Linux系统上,文件被看作是字节序列
batyes()这个内置函数,这个方法是将字符串转换为字节类型
# utf-8 一个汉子:3个字节 # gbk一个汉子:2个字节, 1bytes = 8bit # 字符串转换字节类型:bytes(只要转换的字符串,按照什么编码) s = "李纳斯" n = bytes(s,encoding="utf-8") print(n) n = bytes(s,encoding="gbk") print(n) # 字节转化成字符串 new_str = str(bytes(s,encoding="utf-8"),encoding="utf-8") print(new_str)
open()内置函数用于文件操作
操作文件时,一般需要经历如下步骤:
- 打开文件
- 操作文件
- 关闭文件
每次都要关闭文件很麻烦,with open(‘文件路径‘,‘模式‘) as filename: 这种方式来打开文件。
#‘r‘为只读,‘1.txt‘为文件路径 f=open(‘1.txt‘,‘r‘,encoding=‘utf-8‘) #打开文件 data=f.read() #操作文件 f.close() #关闭文件 print(data) # 用with语句打开,不需要自动关闭 with open(‘1.txt‘,‘r‘,encoding=‘utf-8‘) as f: print(f.read())
一、打开文件
文件句柄 = open(file, mode=‘r‘, buffering=-1, encoding=None)
打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作。
打开文件的模式有:
- r ,只读模式【默认】
- w,只写模式【不可读;不存在则创建;存在则清空内容;】
- x, 只写模式【不可读;不存在则创建,存在则报错】
- a, 追加模式【可读; 不存在则创建;存在则只追加内容;】
"+" 表示可以同时读写某个文件
- r+, 读写【可读,可写】
- w+,写读【可读,可写】
- x+ ,写读【可读,可写】
- a+, 写读【可读,可写】
"b"表示以字节的方式操作
- rb 或 r+b
- wb 或 w+b
- xb 或 w+b
- ab 或 a+b
注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型,当以二进制的方式的效率比较高,因为磁盘底层是以字节存储
二、操作
class file(object) def close(self): # real signature unknown; restored from __doc__ 关闭文件 """ close() -> None or (perhaps) an integer. Close the file. Sets data attribute .closed to True. A closed file cannot be used for further I/O operations. close() may be called more than once without error. Some kinds of file objects (for example, opened by popen()) may return an exit status upon closing. """ 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设备 """ isatty() -> true or false. True if the file is connected to a tty device. """ return False def next(self): # real signature unknown; restored from __doc__ 获取下一行数据,不存在,则报错 """ x.next() -> the next value, or raise StopIteration """ pass def read(self, size=None): # real signature unknown; restored from __doc__ 读取指定字节数据 """ read([size]) -> read at most size bytes, returned as a string. If the size argument is negative or omitted, read until EOF is reached. Notice that when in non-blocking mode, less data than what was requested may be returned, even if no size parameter was given. """ pass def readinto(self): # real signature unknown; restored from __doc__ 读取到缓冲区,不要用,将被遗弃 """ readinto() -> Undocumented. Don‘t use this; it may go away. """ pass def readline(self, size=None): # real signature unknown; restored from __doc__ 仅读取一行数据 """ readline([size]) -> next line from the file, as a string. Retain newline. A non-negative size argument limits the maximum number of bytes to return (an incomplete line may be returned then). Return an empty string at EOF. """ pass def readlines(self, size=None): # real signature unknown; restored from __doc__ 读取所有数据,并根据换行保存值列表 """ readlines([size]) -> list of strings, each a line from the file. Call readline() repeatedly and return a list of the lines so read. The optional size argument, if given, is an approximate bound on the total number of bytes in the lines returned. """ return [] def seek(self, offset, whence=None): # real signature unknown; restored from __doc__ 指定文件中指针位置 """ seek(offset[, whence]) -> None. Move to new file position. Argument offset is a byte count. Optional argument whence defaults to (offset from start of file, offset should be >= 0); other values are 1 (move relative to current position, positive or negative), and 2 (move relative to end of file, usually negative, although many platforms allow seeking beyond the end of a file). If the file is opened in text mode, only offsets returned by tell() are legal. Use of other offsets causes undefined behavior. Note that not all file objects are seekable. """ pass def tell(self): # real signature unknown; restored from __doc__ 获取当前指针位置 """ tell() -> current file position, an integer (may be a long integer). """ pass def truncate(self, size=None): # real signature unknown; restored from __doc__ 截断数据,仅保留指定之前数据 """ truncate([size]) -> None. Truncate the file to at most size bytes. Size defaults to the current file position, as returned by tell(). """ pass def write(self, p_str): # real signature unknown; restored from __doc__ 写内容 """ write(str) -> None. Write string str to file. Note that due to buffering, flush() or close() may be needed before the file on disk reflects the data written. """ pass def writelines(self, sequence_of_strings): # real signature unknown; restored from __doc__ 将一个字符串列表写入文件 """ writelines(sequence_of_strings) -> None. Write the strings to the file. Note that newlines are not added. The sequence can be any iterable object producing strings. This is equivalent to calling write() for each string. """ pass def xreadlines(self): # real signature unknown; restored from __doc__ 可用于逐行读取文件,非全部 """ xreadlines() -> returns self. For backward compatibility. File objects now include the performance optimizations previously implemented in the xreadlines module. """ pass 2.x
2.x
class TextIOWrapper(_TextIOBase): """ Character and line based layer over a BufferedIOBase object, buffer. encoding gives the name of the encoding that the stream will be decoded or encoded with. It defaults to locale.getpreferredencoding(False). errors determines the strictness of encoding and decoding (see help(codecs.Codec) or the documentation for codecs.register) and defaults to "strict". newline controls how line endings are handled. It can be None, ‘‘, ‘\n‘, ‘\r‘, and ‘\r\n‘. It works as follows: * On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in ‘\n‘, ‘\r‘, or ‘\r\n‘, and these are translated into ‘\n‘ before being returned to the caller. If it is ‘‘, universal newline mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated. * On output, if newline is None, any ‘\n‘ characters written are translated to the system default line separator, os.linesep. If newline is ‘‘ or ‘\n‘, no translation takes place. If newline is any of the other legal values, any ‘\n‘ characters written are translated to the given string. If line_buffering is True, a call to flush is implied when a call to write contains a newline character. """ def close(self, *args, **kwargs): # real signature unknown 关闭文件 pass def fileno(self, *args, **kwargs): # real signature unknown 文件描述符 pass def flush(self, *args, **kwargs): # real signature unknown 刷新文件内部缓冲区 pass def isatty(self, *args, **kwargs): # real signature unknown 判断文件是否是同意tty设备 pass def read(self, *args, **kwargs): # real signature unknown 读取指定字节数据 pass def readable(self, *args, **kwargs): # real signature unknown 是否可读 pass def readline(self, *args, **kwargs): # real signature unknown 仅读取一行数据 pass def seek(self, *args, **kwargs): # real signature unknown 指定文件中指针位置 pass def seekable(self, *args, **kwargs): # real signature unknown 指针是否可操作 pass def tell(self, *args, **kwargs): # real signature unknown 获取指针位置 pass def truncate(self, *args, **kwargs): # real signature unknown 截断数据,仅保留指定之前数据 pass def writable(self, *args, **kwargs): # real signature unknown 是否可写 pass def write(self, *args, **kwargs): # real signature unknown 写内容 pass def __getstate__(self, *args, **kwargs): # real signature unknown pass def __init__(self, *args, **kwargs): # real signature unknown pass @staticmethod # known case of __new__ def __new__(*args, **kwargs): # real signature unknown """ Create and return a new object. See help(type) for accurate signature. """ pass def __next__(self, *args, **kwargs): # real signature unknown """ Implement next(self). """ pass def __repr__(self, *args, **kwargs): # real signature unknown """ Return repr(self). """ pass buffer = property(lambda self: object(), lambda self, v: None, lambda self: None) # default closed = property(lambda self: object(), lambda self, v: None, lambda self: None) # default encoding = property(lambda self: object(), lambda self, v: None, lambda self: None) # default errors = property(lambda self: object(), lambda self, v: None, lambda self: None) # default line_buffering = property(lambda self: object(), lambda self, v: None, lambda self: None) # default name = property(lambda self: object(), lambda self, v: None, lambda self: None) # default newlines = property(lambda self: object(), lambda self, v: None, lambda self: None) # default _CHUNK_SIZE = property(lambda self: object(), lambda self, v: None, lambda self: None) # default _finalizing = property(lambda self: object(), lambda self, v: None, lambda self: None) # default 3.x
3.x
f.close() 关闭文件 f.fileno() 返回文件描述符 f.readline() 从当前指针读取一行 f.readlines() 从当前指针读取到结尾的全部行 f.read() 从当前指针读多少个字节,没有参数读取全部 f.tell() 告诉当前指针,是字节 f.seek(offset [whence]) 移动指针,f.seek(0)把指针移动第一行第0个字节位置 offset: 偏移量 whence: 位置 0: 从文件头 1:从当前位置 2:从文件尾部 f.write(string) 打开文件时,文件不存在,r,r+都会报错,其他模式则不会 f.writelines() 必须是字符串序列,把字符串序列当作一个列表写进文件 f.flush() 在文件没有关闭时,可以将内存中的数据刷写至磁盘 f.truncate() 文件截取多少字节保留,指针后面的内容全部会清空 f.name 是返回文件名字,不是方法,是属性 f.closed 判断文件是否已经关闭 f.encoding 查看编码格式,没有使用任何编码,则为None f.mode 打开文件的模式 f.newlines 显示出换行符的,空为默认\n不显示
一些例子:
#打开模式没有b则打开的字符串 f = open(‘db‘,‘r‘) data = f.read() print(data,type(data)) f.close() #打开模式有b则打开的时字节 f1 = open(‘db‘,‘rb‘) data = f.read() print(data,type(data)) f.close() # 打开模式有w和b则写入的字符串需要转换成字节 f2 = open(‘db‘,‘ab‘) f.write(bytes("hello",encoding="utf-8")) f.close() f3 = open(‘db‘,‘a+‘,encoding="utf-8") # 如果打开模式无b,则read,按照字符读取 data = f3.read(1) # tell当前指针所在的位置(字节) print(f3.tell()) # 调整当前指针你的位置(字节),写的话会覆盖原来的 f3.seek(f3.tell()) f3.write("777") f3.close()
三、管理上下文
为了避免打开文件后忘记关闭,可以通过管理上下文,即:
with open(‘log‘,‘r‘) as f: ...
如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。
在Python 2.7 及以后,with又支持同时对多个文件的上下文进行管理,即:
(1)读取一个文件中的10行写入另外一个文件中
with open(‘db1‘,‘r‘,encoding="utf-8") as f1,open(‘db2‘,‘w‘,encoding="utf-8") as f2: times = 0 for line in f1: times += 1 if times <= 10: f2.write(line) else: break
(2)将一个文件一行一行读取并批量替换并写入另外一个文件
with open(‘db1‘,‘r‘,encoding="utf-8") as f1,open(‘db2‘,‘w‘,encoding="utf-8") as f2: for line in f1: new_str = line.replace(‘ales‘,‘st‘) f2.write(new_str)
(3)假设现在有这样一个需求,有一个10G大的文件,如何拷贝到另一个文件中去?下面将讲一下如何同时打开两个文件进行处理,以及文件太大的时候如何读取用with语句就可以同时打开两个文件,一个读,一个写。假设1.txt文件有10G大,如果用read则一次性就将内容读到内存中去了,这显然不合适,如果用readline()的话虽然也可以读一行,但是并不知道何时结束,但是可以用for循环读取文件这个可迭代的对象,一行行的读取。下面三行代码就可以实现了
with open(‘1.txt‘,‘r‘,encoding=‘utf-8‘) as fread,open(‘2.txt‘,‘w‘) as fwrite: for line in fread: #一行行的读 fwrite.write(line) #一行行的写
大量练习:
示例: >>> import os >>> os.system(‘cp /etc/passwd /tmp/passwd‘) >>> f1 = open(‘/tmp/passwd‘,‘r‘) >>> type(f1) >>> file >>> f1.next() >>> ‘root:x:0:0:root:/root:/bin/bash\n‘ 示例: >>> f1 = open(‘/tmp/passwd‘,‘r+‘) >>> f1.next() ‘root:x:0:0:root:/root:/bin/bash\n‘ >>> f1.seek(0,2) >>> f1.tell() 927 >>> f1.write(‘new line.\n‘) >>> f1.tell() 937 >>> f1.close() 示例:带+号可以避免文件不存在报错 >>> import os >>> os.system(‘cp /etc/shadow /tmp/shadow‘) >>> f2 = open(‘/tmp/shadow‘,‘w+‘) >>> f2.write(‘hello‘) >>> f2.close() 示例:如果是只读,文件不存在则报错 >>> f3 = open(‘/tmp/test.txt‘,‘r‘) --------------------------------------------------------------------------- IOError Traceback (most recent call last) <ipython-input-12-d21a831010b6> in <module>() ----> 1 f3 = open(‘/tmp/test.txt‘,‘r‘) IOError: [Errno 2] No such file or directory: ‘/tmp/test.txt‘ 注意:如果文件不存在,r,r+都会报错 示例: >>> f3 = open(‘/tmp/test.txt1‘,‘w+‘) >>> for line in ( i**2 for i in range(1,11) ): f3.write(str(line)+‘\n‘) >>> f3.flush() >>> f3.close() 示例: >>> import os >>> l2 = os.listdir(‘/etc/‘) >>> f4.writelines(l2) >>> f4.flush() 示例: >>> l3 = [ i+‘\n‘ for i in os.listdir(‘/etc/‘) ] >>> f4.seek(0) >>> f4.writelines(l3) >>> f4.flush() >>> f4.seek(0) >>> f4.writelines([ i+‘\n‘ for i in os.listdir(‘/etc/‘)]) >>> f4.flush()
文件系统功能:import os
目录相关:
os.getcwd() 返回当前工作目录
os.chdir() 切换目录
os.chroot() 设定当前进程的根目录
os.listdir() 列出指定目录下的所有文件名
os.mkdir() 创建指定目录
os.makedirs() 创建多级目录
os.rmdir() 删除陌路
os.removedirs() 删除多级目录
文件相关:
os.mkfifo() 创建管道文件
os.mknod() 创建设备文件
os.remove() 删除文件
os.rename() 文件重命名
os.stat() 查看文件的状态属性
os.symlink() 创建链接文件
os.unlink() 删除链接文件
os.utime() 更新文件时间戳
os.tmpfile() 创建并打开(w+b)一个新的
os.walk() 生成目录结构的生成器
访问权限:
os.access() 检验文件某个用户是否有访问权限
os.chmod() 修改权限
os.chown() 修改属主属组
os.umask() 设置默认权限模式
文件描述符:
os.open() 根据文件描述打开
os.read() 根据文件描述读
os.write() 根据文件描述符写
创建设备:
os.mkdev() 创建设备文件
os.major() 获取设备主版本号
os.minor() 获取设备次版本号
用户相关:
os.getuid() 获取当前用户的uid
os.getgid() 获取当前用户的gid
文件路径:
os.path.basename() 路径基名
os.path.dirname() 路径目录名
os.path.join() 将dirname()和basename()连接起来
os.path.split() 返回dirname(),basename()元组
os.path.splitext() 返回(filename,extension)元组
os.path.getatime()
os.path.getctime()
os.path.getmtime()
os.path.getsize() 返回文件的大小
os.path.exists() 判断指定文件是否存在
os.path.isabs() 判断指定的路径是否为绝对路径
os.path.isdir() 判断是否为目录
os.path.isfile() 判断是否为文件
os.path.islink() 判断是否为链接文件
os.path.ismount() 判断是否为挂载点
os.path.samefile() 判断两个路径是否指向了同一个文件
对象持久存储:
pickle模块
marshal
DMB接口
shelve模块
pickle.dump()
pickle.load()
示例: >>> dir1 = os.path.dirname(‘/etc/sysconfig/network-scripts‘) >>> file1 = os.path.basename(‘/etc/sysconfig/network-scripts‘) >>> print(dir1,file1) /etc/sysconfig network-scripts >>> os.path.join(dir1,file1) ‘/etc/sysconfig/network-scripts‘ 示例: >>> for filename in os.listdir(‘/tmp‘): print(os.path.join(‘/tmp‘,filename)) 示例:判断文件是否存在,存在则打开 让用户通过键盘反复输入多行数据 追加保存至此文件中 #!/usr/bin/env python27 import os import os.path filename = ‘/tmp/test2.txt‘ if os.path.isfile(filename): f1 = open(filename,‘a+‘) while True: line = raw_input(‘Enter somethin> ‘) if line == ‘q‘ or line == ‘quit‘: break f1.write(line+‘\n‘) f1.close() 示例:将字典写入文件中,并还原 >>> import pickle >>> d1 = {‘x‘:123,‘y‘:567,‘z‘:‘hello world‘} >>> f5 = open(‘/tmp/dfile.txt‘,‘a+‘) >>> pickle.dump(d1,f5) >>> f5.flush() >>> f5.close() >>> f6 = open(‘/tmp/dfile.txt‘,‘r‘) >>> d2 = pickle.load(f6) >>> print(d2) {‘y‘: 567, ‘x‘: 123, ‘z‘: ‘hello world‘}