zoukankan      html  css  js  c++  java
  • 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
    ')
    
    ... else:
    
    ... print('File already exists!')
    
    ...
    
    File already exists!

    StringIO | BytesIO

     1 -- StringIO
     2 
     3 >>> s = io.StringIO()
     4 
     5 >>> s.write('Hello World
    ')
     6 
     7 12
     8 
     9 >>> print('This is a test', file=s)
    10 
    11 15
    12 
    13 >>> s.getvalue()
    14 
    15 'Hello World
    This is a test
    '
    16 
    17 >>>
    18 
    19 >>> s = io.StringIO('Hello
    World
    ')
    20 
    21 >>> s.read(4)
    22 
    23 'Hell'
    24 
    25 >>> s.read()
    26 
    27 'o
    World
    '
    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 os
    
    import mmap
    
     
    
    def 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 = 1000000
    
    with 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'}
  • 相关阅读:
    Linux C Socket编程原理及简单实例
    clock_gettime 用法
    Linux未来监控tracing框架——eBPF
    eBPF监控工具bcc系列五工具funccount
    【转】如何测量电源纹波?
    【转】在网页中运行VB6程序
    如何为互阻抗放大器电路选择具有足够带宽的运算放大器
    互阻放大器的稳定工作及其评估
    【原创】OPA857 TEST模式使用
    [转]What you need to know about transimpedance amplifiers – part 1
  • 原文地址:https://www.cnblogs.com/zeopean/p/python.html
Copyright © 2011-2022 走看看