• Python文件


    file=open('filename','mode') 打开文件,filename-文件名,mode模式有r,w,a,rb,wb,w+

    file1=file.read()读文件,括号可加数字,代表读多少字节 readlines()参数可为序列

    file2=file.write()写文件,writelines()参数可为序列

    >>>file=open('b.txt','r+w')
    >>>file.mode
    >>>'w'
    >>>file.name
    >>>'b.txt'
    >>>file.tell()
    >>>0L
    >>>file.seek(2,1)
    >>>file.tell()
    >>>2L
    >>>file.read(128)
    >>>'1234567890'
    >>>file.tell()
    >>>10L
    >>>help('file')
    Help on class file in module __builtin__:

    class file(object)
     |  file(name[, mode[, buffering]]) -> file object
     |
     |  Open a file.  The mode can be 'r', 'w' or 'a' for reading (default),
     |  writing or appending.  The file will be created if it doesn't exist
     |  when opened for writing or appending; it will be truncated when
     |  opened for writing.  Add a 'b' to the mode for binary files.
     |  Add a '+' to the mode to allow simultaneous reading and writing.
     |  If the buffering argument is given, 0 means unbuffered, 1 means line
     |  buffered, and larger numbers specify the buffer size.  The preferred way
     |  to open a file is with the builtin open() function.
     |  Add a 'U' to mode to open the file for input with universal newline
     |  support.  Any line ending in the input file will be seen as a '\n'
     |  in Python.  Also, a file so opened gains the attribute 'newlines';
     |  the value for this attribute is one of None (no newline read yet),
     |  '\r', '\n', '\r\n' or a tuple containing all the newline types seen.
     |
     |  'U' cannot be combined with 'w' or '+' mode.
     |
     |  Methods defined here:
     |
     |  __delattr__(...)
    :
    Character Meaning
    'r' open for reading (default)
    'w' open for writing, truncating the file first
    'a' open for writing, appending to the end of the file if it exists
    'b' binary mode
    't' text mode (default)
    '+' open a disk file for updating (reading and writing)
    'U'

    universal newline mode (for backwards compatibility; should not be used in new code)

    The mode can be 'r', 'w' or 'a' for reading (default), writing, or appending. The file will be created if it doesn’t exist when opened for writing or appending; it will be truncated when opened for writing. Add a '+' to the mode to allow simultaneous reading and writing.

    read([n])
    Read and return at most n bytes. Only one system call is made, so it is possible that less data than was requested is returned. Use len() on the returned bytes object to see how many bytes were actually returned. (In non-blocking mode, None is returned when no data is available.)
    readall()
    Read and return the entire file’s contents in a single bytes object. As much as immediately available is returned in non-blocking mode. If the EOF has been reached, b'' is returned.
    write(b)
    Write the bytes or bytearray object, b, to the file, and return the number actually written. Only one system call is made, so it is possible that only some of the data is written.


    打开一个文件并向其写入内容

    Python的open方法用来打开一个文件。第一个参数是文件的位置和文件名,第二个参数是读写模式。

    我们采用w模式,也就是写模式。在这种模式下,文件原有的内容将会被删除。

    向文件添加内容

    在open的时候制定'a'即为(append)模式,在这种模式下,文件的原有内容不会消失,新写入的内容会自动被添加到文件的末尾。

    读文件内容

    在open的时候制定'r'即为读取模式

    有点乱...  :(

  • 相关阅读:
    变量声明和定义的关系
    STL之Vector
    STL之顺序容器
    类成员函数可以为回调函数吗
    排序
    名字查找
    Java 写 Log
    Spring 框架中注释驱动的事件监听器详解
    Centos7 安装gitLab
    世界经济史是一部基于假象和谎言的连续剧
  • 原文地址:https://www.cnblogs.com/SouthRain/p/2255691.html
Copyright © 2020-2023  润新知