open函数,该函数用于文件处理

操作文件时,一般需要经历如下步骤:

  • 打开文件
  • 操作文件

一、打开文件

1

文件句柄 = open('文件路径', '模式')

打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作。

打开文件的模式有:

  • r,只读模式(默认)。
  • w,只写模式。【不可读;不存在则创建;存在则删除内容;】
  • a,追加模式。【可读;   不存在则创建;存在则只追加内容;】

"+" 表示可以同时读写某个文件

  • r+,可读写文件。【可读;可写;可追加】
  • w+,写读
  • a+,同a

"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)

  • rU
  • r+U

"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)

  • rb
  • wb
  • ab
  • class TextIOWrapper(_TextIOBase):

    """

    Character and line based layer over a BufferedIOBase object, buffer.

    encoding gives the name of the encoding that the stream will be

    decoded or encoded with. It defaults to locale.getpreferredencoding(False).

    errors determines the strictness of encoding and decoding (see

    help(codecs.Codec) or the documentation for codecs.register) and

    defaults to "strict".

    newline controls how line endings are handled. It can be None, '',

    '\n', '\r', and '\r\n'.  It works as follows:

    * On input, if newline is None, universal newlines mode is

    enabled. Lines in the input can end in '\n', '\r', or '\r\n', and

    these are translated into '\n' before being returned to the

    caller. If it is '', universal newline mode is enabled, but line

    endings are returned to the caller untranslated. If it has any of

    the other legal values, input lines are only terminated by the given

    string, and the line ending is returned to the caller untranslated.

    * On output, if newline is None, any '\n' characters written are

    translated to the system default line separator, os.linesep. If

    newline is '' or '\n', no translation takes place. If newline is any

    of the other legal values, any '\n' characters written are translated

    to the given string.

    If line_buffering is True, a call to flush is implied when a call to

    write contains a newline character.

    """

    def close(self, *args, **kwargs): # real signature unknown

    关闭文件

    pass

    def fileno(self, *args, **kwargs): # real signature unknown

    文件描述符

    pass

    def flush(self, *args, **kwargs): # real signature unknown

    刷新文件内部缓冲区

    pass

    def isatty(self, *args, **kwargs): # real signature unknown

    判断文件是否是同意tty设备

    pass

    def read(self, *args, **kwargs): # real signature unknown

    读取指定字节数据

    pass

    def readable(self, *args, **kwargs): # real signature unknown

    是否可读

    pass

    def readline(self, *args, **kwargs): # real signature unknown

    仅读取一行数据

    pass

    def seek(self, *args, **kwargs): # real signature unknown

    指定文件中指针位置

    pass

    def seekable(self, *args, **kwargs): # real signature unknown

    指针是否可操作

    pass

    def tell(self, *args, **kwargs): # real signature unknown

    获取指针位置

    pass

    def truncate(self, *args, **kwargs): # real signature unknown

    截断数据,仅保留指定之前数据

    pass

    def writable(self, *args, **kwargs): # real signature unknown

    是否可写

    pass

    def write(self, *args, **kwargs): # real signature unknown

    写内容

    pass

    def __getstate__(self, *args, **kwargs): # real signature unknown

    pass

    def __init__(self, *args, **kwargs): # real signature unknown

    pass

    @staticmethod # known case of __new__

    def __new__(*args, **kwargs): # real signature unknown

    """ Create and return a new object.  See help(type) for accurate signature. """

    pass

    def __next__(self, *args, **kwargs): # real signature unknown

    """ Implement next(self). """

    pass

    def __repr__(self, *args, **kwargs): # real signature unknown

    """ Return repr(self). """

    pass

    buffer = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    closed = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    encoding = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    errors = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    line_buffering = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    name = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    newlines = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    _CHUNK_SIZE = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    _finalizing = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

python-open函数的更多相关文章

  1. python的函数

    函数一词起源于数学,但是在编程中的函数和数学中的有很大不同.编程中的函数式组织好的,可重复使用的,用于实现单一功能或相关联功能的代码块. 我们在学习过程中已经使用过一些python内建的函数,如pri ...

  2. python strip()函数 介绍

    python strip()函数 介绍,需要的朋友可以参考一下   函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm)        删除s字符串中开头.结尾处,位于 rm删除 ...

  3. python split()函数

    Python split()函数 函数原型: split([char][, num])默认用空格分割,参数char为分割字符,num为分割次数,即分割成(num+1)个字符串 1.按某一个字符分割. ...

  4. Python数学函数

    1.Python数学函数 1.abs(x):取绝对值,内建函数 2.math.ceil(x):向上取整,在math模块中 3.cmp(x,y):如果 x < y ,返回-1:如果 x == y ...

  5. Python回调函数用法实例详解

    本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...

  6. Python之函数与变量

    本节内容 函数介绍及其作用 函数的定义与调用 函数的参数说明 全局变量与局部变量 值传递和引用传递 一.函数的介绍及其作用 编程语言中的函数与数学中的函数是有区别的:数学中的函数有参数(输入),就会有 ...

  7. Python基础-函数篇

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数  函数与函数式编程 1.面向对象: 华山派-- ...

  8. 【C++实现python字符串函数库】strip、lstrip、rstrip方法

    [C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...

  9. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  10. 【C++实现python字符串函数库】一:分割函数:split、rsplit

    [C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...

随机推荐

  1. JavaWeb_(Spring框架)SpringAOP面向切面编程

    SpringAOP:面向切面编程(面向fifter编程) 通俗易懂术语:所有纵向重复的代码,我们提取成横向的代码 以下文章内容参考知乎:从0带你学习SpringAOP,彻底的理解AOP思想 传送门 1 ...

  2. CodeForces 754C Vladik and chat ——(xjbg)

    虽然是xjbg的题目,但是并不很好做. 题意不难理解.读入有点麻烦.做法是先正着推每段对话的?可能是谁说的,然后反过来选择即可.正推时,其中vis数组表示谁已经被用过了,cnt表示该组当前可以选择几个 ...

  3. chrome获取xpath元素-f12工具

    Chrome浏览器获取XPATH的方法----通过开发者工具获取 引用源:https://blog.csdn.net/li6727975/article/details/46126079   版权声明 ...

  4. ORA-12899 导入失败

    主要是目标数据库的字符集与导入文件的字符集不符 SQL>SHUTDOWN IMMEDIATE SQL>STARTUP MOUNT SQL>ALTER SYSTEM ENABLE RE ...

  5. 解决“mysql不是内部/外部命令,也不是可执行程序,也不是批处理文件”

    解决方案: 1.切换到mysql.exe文件所在目录: 2.将mysql.exe文件所在目录添加到操作系统内的环境变量中: 如何添加环境变量: 1.右击“我的电脑”——>属性——>高级—— ...

  6. 自动更新文件夹下所有DLL 至最新修改时间版本

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. [go]匿名结构体

    匿名结构体声明 使用匿名结构体 声明并初始化

  8. SSH整合入门案例

    package loaderman.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import ...

  9. MOCK服务小结

    前言: 说到mock,大家会想到单测中的mock,测试同学会想到httpmock服务等. mock的作用:程序运行过程中,设定过滤规则及返回值,来满足固定的数据解析,解决不容易构造或者获取的数据对象. ...

  10. Jedis的Publish/Subscribe功能的使用

    redis内置了发布/订阅功能,可以作为消息机制使用.所以这里主要使用Jedis的Publish/Subscribe功能. 1.使用Spring来配置Jedis连接池 <!-- pool配置 - ...