文件系统就是磁盘上组织文件的方法。
要存储文件,需要序列化。
Python内置函数open()用于打开文件和创建文件对象。
open(name[.mode[.bufsize]])
接受三个参数:文件名,模式(读,只读。。。),缓冲区参数(0表示无缓冲,1表示使用缓冲只缓冲一行,负数表示使用系统默认,正数表示使用指定大小)
访问模式:
简单模式:r :只读 w:写入 a:附加
模式后使用+号表示同时支持输入、输出操作r+、w+、a+
附加b表示以二进制打开 rb, wb+
文件读取类似指针,依次读取文件内容。读取到尾部,需要重置指针。
In [313]: f1.tell()
Out[313]: 2053 字节数seek(offset[,whence]) 移动指针。接受偏移量,和开始偏移位置。
whence:0--从文件头
1--从当前位置
2--从文件尾部
f.seek(0) 移动到文件头。
f.read()指定读取多少字节数据
In [319]: f1.read(10)
Out[319]: 'root:x:0:0'
In [320]: f1.name
Out[320]: '/etc/passwd'
以只读模式打开一个不存在的文件则会报错。以写模式和附加模式则不会。使用r+ 也不能够打开,因为使用的是读模式。
In [27]: l3 = [ i + '\n' for i in os.listdir('/etc/') ]
In [31]: f4.writelines([ i + '\n' for i in os.listdir('/etc/') ])
In [32]: f4.flush()
file.isatty() 是否是终端设备文件
file.truncate() 截取自定义长度字节 file.truncate(f4.tell()) 截取到指针位置
迭代next的方式不会移动指针,readline 可以。
file.newlines() 行分隔符
文件系统功能:os模块实现
os.mkdir() 创建目录
In [35]: os.getcwd()
Out[35]: '/usr/ccc/exp'In [36]: os.chdir('/tmp/')
In [37]: os.getcwd()
Out[37]: '/tmp'
In [38]: os.stat('/tmp/test.txt')
Out[38]: posix.stat_result(st_mode=33188, st_ino=103935292, st_dev=64768L, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1449394094, st_mtime=1449394080, st_ctime=1449394080)fchdir() 通过文件描述付改变目录;chroot() 设定当前进程的根目录;listdir() 列出指定目录下的所有文件名;mkdir() 创建指定目录;makedirs() 创建多级目录,创建的目录父目录不存在,则先创建父目录;rmdir()删除目录;removedirs()删除多级目录。
文件:
mkfifo() 创建命名管道,先进先出管道;mknod() 创建设备文件;remove() 删除文件;unlink()删除链接文件;rename();stat() 返回文件状态信息;symlink() 创建符号链接;
utime() 更新时间戳;tmpfile() 创建并打开(w+b)一个新的临时文件;os.walk() 目录树生成器;
访问权限:
access() 判定指定用户对某文件是否有权限;chmod() 修改权限;chown() 改变属主,属组;
In [50]: os.chown('test2.txt',1001,1001)
umask() 设置默认权限模式;
文件描述符:
open() ;read();write();
设备文件:
makedev();
makedev(...)
makedev(major, minor) -> device number Composes a raw device number from the major and minor device numbers.os.path.*** 实现路径管理
In [53]: import os.path
In [54]: os.path.
os.path.abspath os.path.joinos.path.altsep os.path.lexistsos.path.basename os.path.normcaseos.path.commonprefix os.path.normpathos.path.curdir os.path.osos.path.defpath os.path.pardiros.path.devnull os.path.pathsepos.path.dirname os.path.realpathos.path.exists os.path.relpathos.path.expanduser os.path.samefileos.path.expandvars os.path.sameopenfileos.path.extsep os.path.samestatos.path.genericpath os.path.sepos.path.getatime os.path.splitos.path.getctime os.path.splitdriveos.path.getmtime os.path.splitextos.path.getsize os.path.statos.path.isabs os.path.supports_unicode_filenamesos.path.isdir os.path.sysos.path.isfile os.path.walkos.path.islink os.path.warningsos.path.ismount
basename() 文件基名; dirname() 路径目录名;join() ; split() 返回dirname() basename() 元组; splitext() 返回(filename,extension) 元组;
信息:
getatime();getctime();getmtime();getsize();
查询:
exists():判断指定文件是否存在;
isabs() :判断指定路径是否为绝对路径;
isdir()是否存在且为目录 ; isfile() 是否存在且为文件; ismount()是否为挂载点;
samefile() 两个路径是否指向了同一文件;
练习:判断文件是否存在,存在则打开并让用户输入多次再保存。
n/python27
#import osimport os.pathfilename = '/tmp/test.txt'
if os.path.isfile(filename):
f1 = open(filename,'a+')while True:
line = raw_input('Enter something>') if line == 'q' or line == 'quet': break f1.write(line+'\n')f1.close()
实现对象持久存储:
保存原有数据结构,需要扁平化,流式化。
pickle
In [56]: d1 = {'a':123,'b':234,'c':'hehllo'}
In [57]: f5 = open('/tmp/dfile.txt','a+')
In [58]: pickle.dump(d1,f5)
In [59]: f5.flush()
In [60]: f5.close()
In [61]: f6 = open('/tmp/dfile.txt','r')
In [62]: d2 = pickle.load(f6)
In [63]: print d2
{'a': 123, 'c': 'hehllo', 'b': 234}marshal
DBM:接口:
shelve模块