zoukankan      html  css  js  c++  java
  • 【python】IO系列

    1. 文件IO

    1.1 file read

    样本C:FILEifconfig.txt

    root@webserver1 test# ifconfig
    eth0      Link encap:Ethernet  HWaddr 00:50:56:A2:14:55  
              inet addr:10.180.137.68  Bcast:10.180.255.255  Mask:255.255.0.0
              inet6 addr: fe80::250:56ff:fea2:1455/64 Scope:Link
    file.readline([size])
    读取指定size bytes的整行,返回字符串
    with open("C:FILEifconfig.txt") as f:
        print f.name   
        print repr(f.readline())
    
    

    执行结果

    C:Python27python.exe C:/PycharmProjects/p3/src/pyproject1/iotest/fileIOtest.py
    C:FILEifconfig.txt
    '[root@webserver1 test]# ifconfig
    '

    file.read([size])

    读取指定size bytes的所有直到EOF,返回字符串 
    with open("C:FILEifconfig.txt") as f:
        print f.name
        print repr(f.read())    

     运行结果

    C:Python27python.exe C:/PycharmProjects/p3/src/pyproject1/iotest/fileIOtest.py
    C:FILEifconfig.txt
    'root@webserver1 test# ifconfig
    eth0      Link encap:Ethernet  HWaddr 00:50:56:A2:14:55  
              inet addr:10.180.137.68  Bcast:10.180.255.255  Mask:255.255.0.0
              inet6 addr: fe80::250:56ff:fea2:1455/64 Scope:Link
     '
    
    Process finished with exit code 0

    file.readlines([sizehint]) 

    读取指定size bytes的所有直到EOF,返回每行构成的list
    with open("C:FILEifconfig.txt") as f:
        print f.name
        print repr(f.readlines())

    运行结果

    C:Python27python.exe C:/PycharmProjects/p3/src/pyproject1/iotest/fileIOtest.py
    C:FILEifconfig.txt
    ['root@webserver1 test# ifconfig
    ', 'eth0      Link encap:Ethernet  HWaddr 00:50:56:A2:14:55  
    ', '          inet addr:10.180.137.68  Bcast:10.180.255.255  Mask:255.255.0.0
    ', '          inet6 addr: fe80::250:56ff:fea2:1455/64 Scope:Link
    ', ' ']

    1.2 file write 

    读写模式

    r只读,r+读写,不创建
    w新建只写,w+新建读写,二者都会将文件内容清零
    (以w方式打开,不能读出。w+可读写)
    **w+与r+区别:
    r+:可读可写,若文件不存在,报错;w+: 可读可写,若文件不存在,创建
    
    以a,a+的方式打开文件,附加方式打开
    (a:附加写方式打开,不可读;a+: 附加读写方式打开)

    file.write(str)

    f = open("C:FILEifconfig2.txt", 'w+')
    f.write("foooo" + "
    ")
    f.write("barr")
    f.close()
    C:FILEifconfig2.txt
    foooo
    barr

    file.writelines(sequence) 

    f = open("C:FILEifconfig2.txt", 'w+')
    towrite = ["ffffffoooo
    ","barbarbar
    ","foobarfoobar"]
    f.writelines(towrite)
    f.close()

     C:FILEifconfig2.txt

    ffffffoooo
    barbarbar
    foobarfoobar

     2. file-like Object

    像open()函数返回的这种有个read()方法的对象,在Python中统称为file-like Object。除了file外,还可以是内存的字节流,网络流,自定义流等等。file-like Object不要求从特定类继承,只要写个read()方法就行。

    StringIO就是在内存中创建的file-like Object,常用作临时缓冲。

  • 相关阅读:
    第一次离职了
    华为手机 关于Android真机调试的时候logcat中无法输出调试信息的解决办法
    ListView的简单实用
    博客生涯开始
    hdu1151:Air Raid(最小路径覆盖)
    hdu1151:Air Raid(最小路径覆盖)
    hdu1162:Eddy's picture(最小生成树)
    hdu1162:Eddy's picture(最小生成树)
    hdu1301:Jungle Roads(最小生成树)
    hdu1301:Jungle Roads(最小生成树)
  • 原文地址:https://www.cnblogs.com/AlexBai326/p/6835037.html
Copyright © 2011-2022 走看看