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. ARP 之 发送请求arp_solicit

    概述 arp_solicit用来发送ARP请求,首先会根据ARP_ANNOUNCE参数来选取源地址,然后判断是否达到内核发送次数上限,未达到则调用内核arp_send_dst函数发送,如果达到上限,则 ...

  2. 外网访问内网的FTP服务器

    转自 外网访问内网的FTP服务器 首先感谢作者给出的总结,原文是介绍Serv-U的,我针对FileZilla Server进行了稍微修改,仅看操作可直接跳到分割线后第3部分. 1. 背景简介最近研究如 ...

  3. 之前有面试到两个日期的大小比较方式,现在整理一下几种方法。   例子:   String beginTime=new String("2017-06-09 10:22:22");     String endTime=new String("2017-05-08 11:22:22");  1  直接用Date自带方法before()和after()比较 SimpleDateFormat d

    各种数据类型(日期/时间.integer.floating point和numeric)转换成格式化的字符串以及反过来从格式化的字符串转换成指定的数据类型.下面列出了这些函数,它们都遵循一个公共的调用 ...

  4. Qt 插件学习(一)

    插件是什么 注意:这儿暂时不考虑静态插件(潜意识中总觉得它根本就不算插件). 插件是一个动态库(共享库).动态库是一个独立的文件中的独立模块,可被多个程序访问. 先看动态库的两种用法 1. 程序链接时 ...

  5. phpstorm配置了git后Terminal 不能使用显示:git' 不是内部或外部命令,也不是可运行的程序

    问题:在phpstorm上配置好git后,将代码拉了下来 ,但是命令行无法使用显示如图 解决方法:①找到安装git的位置,然后在该目录的子目录下分别找到git-core.bin 两个目录,我的安装在了 ...

  6. C++ UFT-8和GB2312间的转换

    在这个帖子找到的代码 还蛮好用的 https://bbs.csdn.net/topics/391040755 #include <codecvt> #include <locale& ...

  7. Nginx URL重写(rewrite)配置及信息详解

    URL重写有利于网站首选域的确定,对于同一资源页面多条路径的301重定向有助于URL权重的集中 Nginx URL重写(rewrite)介绍 和apache等web服务软件一样,rewrite的组要功 ...

  8. Linux服务器集群性能监控之Performance Co-Pilot(PCP)部署

    转载自:https://blog.csdn.net/w84268426/article/details/78431778 在部署PCP时,我用到了两台cent os 7虚拟机. 1.官方安装文档htt ...

  9. Linux系统管理_主题02 :管好文件(1)_2.4 链接文件_ln

    在 Linux 中,链接有两种:符号链接(symbolic link)和硬链接(hard link).  删除一个符号链接不会影响到这个符号链接指向的目标文件或目 录:  反过来,删除.移动或者重 ...

  10. RocketMQ之十:RocketMQ消息接收源码

    1. 简介 1.1.接收消息 RebalanceService:均衡消息队列服务,负责通过MQClientInstance分配当前 Consumer 可消费的消息队列( MessageQueue ). ...