实际案例
将文件内容写入到硬件设备时,使用系统调用,这类I/O操作的时间很长。为了减少I/O操作的次数,文件通常使用缓冲区,当有足够多的数据时才进行系统调用。文件的缓冲行为,分为全缓冲、行缓冲和无缓冲。
文件缓冲 分为全缓冲(硬盘 缓冲大小满了,就落盘),行缓冲(tty),无缓冲(串口)
全缓冲 : open函数的buffering设置大于1的整数n,n为缓冲区大小 linux默认为page的大小4096 满了n 个字节才会落盘
# mac os 系统不是的
行缓冲 : open 函数的buffering设置为1 f=open("demo.txt",'w',buffering=1) 碰到换行就会将缓冲区落盘
1 2 3 4 5 6 7 8 9 10 |
In [22]: f = open('demo.txt','w',buffering=1) In [23]: f.write('akkkk') Out[23]: 5 In [24]: f.write('\n') Out[24]: 1 In [25]: f.write("hhh\n") Out[25]: 4 |
无缓冲 : open 函数的buffering设置为0 f=open("demo.txt",'w',buffering=0) 时时落盘到硬盘
"""
From open's docstring:
... buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode) ...
So change inFile = open(WORDLIST_FILENAME, 'r', 0)
to
inFile = open(WORDLIST_FILENAME, 'r'), or to
inFile = open(WORDLIST_FILENAME, 'rb', 0) if you really need it (which I doubt).
"""
