python 文件流
生活随笔
收集整理的這篇文章主要介紹了
python 文件流
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
open?|?write?|?read
>>> with open('demo.txt' , 'wt') as f: #以文本(wt)形式寫入操作... f.write("hello guohai")12>>> with open('demo.txt' , 'rt') as f: #以文本(rt)形式讀取操作... data = f.read()>>> data'hello guohai'>>> with open('demo.txt' , 'at') as f: #以文本(at)形式追加操作... f.write('hello zeopean')13>>> with open('demo.txt' , 'rt') as f:... data = f.read()>>> data'hello guohaihello zeopean'-- print 輸出到文件中with open('d:/work/test.txt', 'wt') as f:print('Hello World!', file=f)
?
?
--?二進制讀寫
如果你想從二進制模式的文件中讀取或寫入文本數據,必須確保要進行解碼和編碼
操作。比如:
1 with open('somefile.bin', 'rb') as f: 2 3 data = f.read(16) 4 5 text = data.decode('utf-8') 6 7 with open('somefile.bin', 'wb') as f: 8 9 text = 'Hello World' 10 11 f.write(text.encode('utf-8')) 12 13?
?
--?判斷文件是否存在
>>> import os>>> if not os.path.exists('somefile'):... with open('somefile', 'wt') as f:... f.write('Hello\n')... else:... print('File already exists!')...File already exists!?
?
StringIO?|?BytesIO
1 -- StringIO 2 3 >>> s = io.StringIO() 4 5 >>> s.write('Hello World\n') 6 7 12 8 9 >>> print('This is a test', file=s) 10 11 15 12 13 >>> s.getvalue() 14 15 'Hello World\nThis is a test\n' 16 17 >>> 18 19 >>> s = io.StringIO('Hello\nWorld\n') 20 21 >>> s.read(4) 22 23 'Hell' 24 25 >>> s.read() 26 27 'o\nWorld\n' 28 29 >>> 30 31 32 33 -- BytesIO 34 35 >>> s = io.BytesIO() 36 37 >>> s.write(b'binary data') 38 39 >>> s.getvalue() 40 41 b'binary data' 42 43 >>>?
?
readinto?|?bytearray?|?memoryview
1 record_size = 32 2 3 buf = bytearray(record_size) 4 5 with open('somefile', 'rb') as f: 6 7 while True: 8 9 n = f.readinto(buf) 10 11 if n < record_size: 12 13 break 14 15 16 17 另外有一個有趣特性就是 memoryview ,它可以通過零復制的方式對已存在的緩沖 18 19 區執行切片操作,甚至還能修改它的內容。比如: 20 21 >>> buf 22 23 bytearray(b'Hello World') 24 25 >>> m1 = memoryview(buf) 26 27 >>> m2 = m1[-5:] 28 29 >>> m2 30 31 <memory at 0x100681390> 32 33 >>> m2[:] = b'WORLD' 34 35 >>> buf 36 37 bytearray(b'Hello WORLD')?
?
mmap?
-- 內存映射的二進制文件import osimport mmapdef memory_map(filename , access=mmap.ACCESS_WRITE):size = os.path.getsize(filename)fd = os.open(filename , os.O_RDWR)return mmap.mmap(fd ,size , access=access)size = 1000000with open('data' , 'wb') as f:f.seek(size - 1)f.write(b'\x00')m = memory_map('data')print(len(m))print(m[0:10])# print(b'hello daming' , file=m[0:11])# m.close() with open('data' ,'rb') as f:print(f.read(11))?
?
os.path
--?basename?|?dirname?|?join?|?splitext?|?expanduser
1 import os 2 3 >>> path = 'demo.bin' 4 5 >>> os.path.basename(path) #文件名 6 7 'demo.bin' 8 9 >>> os.path.dirname(path) #文件路徑 10 11 '' 12 13 >>> os.path.join('tmp','data' , os.path.basename(path)) #路徑拼接 14 15 'tmp\\data\\demo.bin' 16 17 >>> path = '~/data/data.bin' 18 19 >>> os.path.expanduser(path) #系統路徑 20 21 'C:\\Users\\Administrator/data/data.bin' 22 23 >>> os.path.splitext(path) #獲取后綴名 24 25 ('~/data/data', '.bin')?
?
--?exists?|?isdir?|?islink?|?realpath
os.path.exists('/etc/passwd') #判斷文件是否存在>>> os.path.isdir('user.data') #判斷是否是目錄 False>>> os.path.islink('demo.bin') #判斷是否是超鏈接 False>>> os.path.realpath('demo.bin') #獲取文件的絕對路徑'E:\\zeopean\\pycode\\pyfunc\\demo.bin'?
?
--?getmtime?|?getsize?獲取元數據
1 >>> os.path.getmtime('demo.txt') 2 3 1459387735.7783203 4 5 >>> import time 6 7 >>> time.ctime(os.path.getmtime('demo.bin')) 8 9 'Thu Mar 31 10:27:36 2016' 10 11 >>> os.path.getsize('demo.txt') 12 13 47?
?
--?listdir?
>>> names = os.listdir('.')>>> names['closure.func.py', 'closureInstance.py', 'data', 'demo.bin', 'demo.txt', 'file. txt', 'make_element.py', 'mmap.demo.py', 'readinto.demo.py', 'yield.func.py']?
?
tempfile
1 --臨時文件 TemporaryFile 2 3 from tempfile import TemporaryFile 4 5 f = TemporaryFile('w+t') 6 7 # Use the temporary file 8 9 ... 10 11 f.close() 12 13 14 15 --有名字的臨時文件 NamedTemporaryFile 16 17 from tempfile import NamedTemporaryFile 18 19 with NamedTemporaryFile('w+t') as f: 20 21 print('filename is:', f.name) 22 23 24 25 -- 臨時目錄 TemporaryDirectory 26 27 from tempfile import TemporaryDirectory 28 29 with TemporaryDirectory() as dirname: 30 31 print('dirname is:', dirname) 32 33 34 35 -- 插件臨時文件 mkstemp | mkdtemp 36 37 >>> import tempfile 38 39 >>> tempfile.mkstemp() 40 41 (3, 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\tmp_jbpp74i') 42 43 >>> tempfile.mkdtemp() 44 45 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\tmpkwsqedpa' 46 47 48 49 -- 獲取臨時文件名 gettempdir 50 51 import tempfile 52 53 tempfile.gettempdir() 54 55 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp'?
?
?
Pickle
1 -- 序列化對象 2 3 >>> import pickle 4 5 >>> f = open('somedata', 'wb') 6 7 >>> pickle.dump([1, 2, 3, 4], f) 8 9 >>> pickle.dump('hello', f) 10 11 >>> pickle.dump({'Apple', 'Pear', 'Banana'}, f) 12 13 >>> f.close() 14 15 >>> f = open('somedata', 'rb') 16 17 >>> pickle.load(f) 18 19 [1, 2, 3, 4] 20 21 >>> pickle.load(f) 22 23 'hello' 24 25 >>> pickle.load(f) 26 27 {'Apple', 'Pear', 'Banana'}?
轉載于:https://www.cnblogs.com/zeopean/p/python.html
總結
以上是生活随笔為你收集整理的python 文件流的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 中的悲观锁和乐观锁的实现
- 下一篇: 基于py3和pymysql的数据库查询,