官方文档

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised.

file is a path-like object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is given, it is closed when the returned I/O object is closed, unless closefd is set to False.)

mode is an optional string that specifies the mode in which the file is opened. It defaults to 'r' which means open for reading in text mode. Other common values are 'w' for writing (truncating the file if it already exists), 'x' for exclusive creation and 'a' for appending (which on some Unix systems, means that all writes append to the end of the file regardless of the current seek position). In text mode, if encoding is not specified the encoding used is platform dependent: locale.getpreferredencoding(False) is called to get the current locale encoding. (For reading and writing raw bytes use binary mode and leave encoding unspecified.) The available modes are:

Character Meaning
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'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 newlines mode (deprecated)

The default mode is 'r' (open for reading text, synonym of 'rt'). For binary read-write access, the mode 'w+b' opens and truncates the file to 0 bytes. 'r+b' opens the file without truncation.

As mentioned in the Overview, Python distinguishes between binary and text I/O. Files opened in binary mode (including 'b' in the mode argument) return contents as bytes objects without any decoding. In text mode (the default, or when 't' is included in the mode argument), the contents of the file are returned as str, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given.

Note

Python doesn’t depend on the underlying operating system’s notion of text files; all the processing is done by Python itself, and is therefore platform-independent.

buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size in bytes of a fixed-size chunk buffer. When no buffering argument is given, the default buffering policy works as follows:

  • Binary files are buffered in fixed-size chunks; the size of the buffer is chosen using a heuristic trying to determine the underlying device’s “block size” and falling back on io.DEFAULT_BUFFER_SIZE. On many systems, the buffer will typically be 4096 or 8192 bytes long.
  • “Interactive” text files (files for which isatty() returns True) use line buffering. Other text files use the policy described above for binary files.

encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent (whatever locale.getpreferredencoding() returns), but any text encoding supported by Python can be used. See the codecs module for the list of supported encodings.

errors is an optional string that specifies how encoding and decoding errors are to be handled—this cannot be used in binary mode. A variety of standard error handlers are available (listed under Error Handlers), though any error handling name that has been registered with codecs.register_error() is also valid. The standard names include:

  • 'strict' to raise a ValueError exception if there is an encoding error. The default value of None has the same effect.
  • 'ignore' ignores errors. Note that ignoring encoding errors can lead to data loss.
  • 'replace' causes a replacement marker (such as '?') to be inserted where there is malformed data.
  • 'surrogateescape' will represent any incorrect bytes as code points in the Unicode Private Use Area ranging from U+DC80 to U+DCFF. These private code points will then be turned back into the same bytes when the surrogateescape error handler is used when writing data. This is useful for processing files in an unknown encoding.
  • 'xmlcharrefreplace' is only supported when writing to a file. Characters not supported by the encoding are replaced with the appropriate XML character reference &#nnn;.
  • 'backslashreplace' replaces malformed data by Python’s backslashed escape sequences.
  • 'namereplace' (also only supported when writing) replaces unsupported characters with \N{...} escape sequences.

newline controls how universal newlines mode works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows:

  • When reading input from the stream, 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 newlines 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.
  • When writing output to the stream, 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 closefd is False and a file descriptor rather than a filename was given, the underlying file descriptor will be kept open when the file is closed. If a filename is given closefd must be True (the default) otherwise an error will be raised.

A custom opener can be used by passing a callable as opener. The underlying file descriptor for the file object is then obtained by calling opener with (file, flags). opener must return an open file descriptor (passing os.open as opener results in functionality similar to passing None).

The newly created file is non-inheritable.

The following example uses the dir_fd parameter of the os.open() function to open a file relative to a given directory:

>>>

>>> import os
>>> dir_fd = os.open('somedir', os.O_RDONLY)
>>> def opener(path, flags):
... return os.open(path, flags, dir_fd=dir_fd)
...
>>> with open('spamspam.txt', 'w', opener=opener) as f:
... print('This will be written to somedir/spamspam.txt', file=f)
...
>>> os.close(dir_fd) # don't leak a file descriptor

The type of file object returned by the open() function depends on the mode. When open() is used to open a file in a text mode ('w', 'r', 'wt', 'rt', etc.), it returns a subclass of io.TextIOBase (specifically io.TextIOWrapper). When used to open a file in a binary mode with buffering, the returned class is a subclass of io.BufferedIOBase. The exact class varies: in read binary mode, it returns an io.BufferedReader; in write binary and append binary modes, it returns an io.BufferedWriter, and in read/write mode, it returns an io.BufferedRandom. When buffering is disabled, the raw stream, a subclass of io.RawIOBase, io.FileIO, is returned.

See also the file handling modules, such as, fileinput, io (where open() is declared), os, os.path, tempfile, and shutil.

Changed in version 3.3:

  • The opener parameter was added.
  • The 'x' mode was added.
  • IOError used to be raised, it is now an alias of OSError.
  • FileExistsError is now raised if the file opened in exclusive creation mode ('x') already exists.
Changed in version 3.4:

  • The file is now non-inheritable.

Deprecated since version 3.4, will be removed in version 4.0: The 'U' mode.

Changed in version 3.5:

  • If the system call is interrupted and the signal handler does not raise an exception, the function now retries the system call instead of raising an InterruptedError exception (see PEP 475 for the rationale).
  • The 'namereplace' error handler was added.
Changed in version 3.6:

说明:1.函数能打开一个文件,返回一个文件读写对象,然后可以对文件进行一个响应的读写操作

         2.file参数表示的需要打开文件的相对路径(当前工作目录)或者一个绝对路径,当传入路径的文件不存在,则会报错,或者传入文件的句柄

#伪代码 open(函数,模式,编码)
#打开文件
obj = open('hello.txt') #相对路径
print(obj)
#输出
#<_io.TextIOWrapper name='hello.txt' mode='r' encoding='UTF-8'> obj = open('/User/desktop/hello2.txt') #绝对路径
print(obj)
#输出
#<_io.TextIOWrapper name='/User/desktop/hello2.txt' mode='r' encoding='UTF-8'>

3.mode参数表示打开文件的模式,常见的模式有如下几种,实际调用的时候可以根据情况进行组合

'r':以只读模式打开(缺省模式)必须保证文件存在,否则报错,该模式下不能使用write()方法

'w':以只写模式打开,如果文件存在则清空内容,重新创建文件,如果文件不存在,则新建文件。使用这个模式必须确保该文件所在的目录是存在的,文件可以不存在,该模式下不能使用read()方法

'a':以追加模式打开,如果文件存在,则追加到文件末尾,如果不存在,则新建文件。该模式下不能使用read()方法

下面的4个模式和上面的模式可以组合使用:

    'b':以二进制的模式打开

    't':以文本的模式打开(缺省)

    '+':以读写模式打开

    'U':以通用换行符模式打开

常见的mode组合:

    'r'或'rt':  默认模式,文本读模式

    'w'或'wt':以文本写模式打开(打开前文件内容会被清空)

    'rb':       以二进制读模式打开

    'wb':      以二进制写模式打开(打开文件前文件内容会被清空)

    'r+':       以文本读写模式打开,可以写在文件任何位置,默认写就是追加,并把指针调到最后

    'w+':        以文本读写模式打开(打开前文件内容会被清空),可以使用read()

    'a+':      以文本读写模式打开(写只能写在文件末尾),可以使用read()

'rb+':     以二进制读写模式打开

'wb+':    以二进制读写模式打开(打开文件前内容会被清空)

    'ab+':    以二进制读写模式打开

#伪代码 open(函数,模式,编码)
#1.打开文件
obj = open('hello.txt') #相对路径
print(obj)
#输出
#<_io.TextIOWrapper name='hello.txt' mode='r' encoding='UTF-8'> obj = open('/User/desktop/hello2.txt') #绝对路径
print(obj)
#输出
#<_io.TextIOWrapper name='/User/desktop/hello2.txt' mode='r' encoding='UTF-8'>
#1.r或者rt:默认模式,文本读模式,文件必须存在,不能使用write(),否则会报错
obj = open('hello.txt','r')
ret = obj.read()
obj.close()
print(ret)
#输出
#hello world

#2.rb:以二进制只读模式打开
obj = open('hello.txt','rb')
ret = obj.read()
obj.close()
print(ret)
#输出
#b'hello world'

#3.w或者wt:以文本写模式打开文件,打开文件前将文件内容清空
obj = open('hello.txt','wt')
obj.write('Python')
obj.close() #4.wb:以二进制写模式打开,打开文件前回将文件内容清空
obj = open('hello.txt','wb')
obj.write(bytes('Hello World',encoding='utf-8' ))
obj.close()

#5.r+:以文本读写模式打开,可以写在文件任何位置,
# 默认写的指针开始指向文件开头,因此会覆盖文件
obj = open('hello.txt','r+')
obj.write('Python')
obj.close() #6.w+:以文本读写模式打开文件,可以使用read()
obj = open('hello.txt','w+')
ret = obj.read()
obj.close()
print(ret) #7.a+:以文本读写模式打开文件(写只能写在文件末尾),可以使用read()
obj = open('hello.txt','a+')
obj.write('Hello World')
obj.close() #8.rb+:以二进制读写模式打开文件,默认写的指针开始指向文件开头,因此会覆盖文件
obj = open('hello.txt','rb+')
obj.write(bytes('我们都一样',encoding='utf-8'))
obj.close() #9.wb+:以二进制读写模式打开(打开文件前内容会被清空)
obj = open('hello.txt','wb+')
obj.write(bytes('张杰',encoding='utf-8'))
obj.close() #10:ab+:以二进制读写模式打开文件
obj = open('hello.txt','ab+')
obj.write(bytes("<我们都一样>",encoding='utf-8'))
obj.close()

4.buffering参数表示文件在读写操作是使用的缓存策略

      0:代表buffer关闭(只适用于二进制模式)

      1:代表line buffer(只适用于文本模式)

      >1:表示初始化的buffer大小

5.encoding参数代表读写文件时所使用的文件编码格式

6.errors参数表示读写文件时碰到错误的报错级别

常见的报错级别有:

  • 'strict' 严格级别,字符编码有报错即抛出异常,也是默认的级别,errors参数值传入None按此级别处理.
  • 'ignore' 忽略级别,字符编码有错,忽略掉.
  • 'replace' 替换级别,字符编码有错的,替换成?.
>>> a = open('test.txt','rt',encoding = 'utf-8')
>>> a.read()
'我是第1行文本,我将被显示在屏幕\n我是第2行文本,我将被显示在屏幕\n我是第3行文本,我将被显示在屏幕'
>>> a = open('test.txt','rt')
>>> a.read()
Traceback (most recent call last):
File "<pyshell#91>", line 1, in <module>
a.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 8: illegal multibyte sequence
>>> a = open('test.txt','rt',errors = 'ignore' )
>>> a.read()
'鎴戞槸绗1琛屾枃鏈锛屾垜灏嗚鏄剧ず鍦ㄥ睆骞\n鎴戞槸绗2琛屾枃鏈锛屾垜灏嗚鏄剧ず鍦ㄥ睆骞\n鎴戞槸绗3琛屾枃鏈锛屾垜灏嗚鏄剧ず鍦ㄥ睆骞'
>>> a = open('test.txt','rt',errors = 'replace' )
>>> a.read()
'鎴戞槸绗�1琛屾枃鏈�锛屾垜灏嗚��鏄剧ず鍦ㄥ睆骞�\n鎴戞槸绗�2琛屾枃鏈�锛屾垜灏嗚��鏄剧ず鍦ㄥ睆骞�\n鎴戞槸绗�3琛屾枃鏈�锛屾垜灏嗚��鏄剧ず鍦ㄥ睆骞�'

7.newline参数表示用于区分换行符(只对文本模式有效,可以取的值有None,'\n','\r','\r\n')

暂时在python3.6.2中没有发现有什么区别,待继续学习

8. closefd表示传入的file参数类型(缺省为True),传入文件路径时一定为True,传入文件句柄则为False。

# a = open('hello.txt','rt',encoding='utf-8',newline='\n',closefd=False)
#报错
#Traceback (most recent call last):
# File "/Users/qianhaichao/Desktop/Python练习/练习项目/老男孩项目实战/open.py", line 80, in <module>
# a = open('hello.txt','rt',encoding='utf-8',newline='\n',closefd=False)
# ValueError: Cannot use closefd=False with file name a = open('hello.txt','rt',encoding='utf-8',newline='\n',closefd=True)

9.tell():获取当前文件的指针

#打开文件
op = open('file1.txt','r+')
#获取r+模式下默认当前文件的指针
print(op.tell())
#读取文件内容
data = op.read()
print('文件内容----:',data)
#获取r+模式下默认当前文件的指针
print(op.tell())
#写入内容
op.write('❤️猪八戒')
#获取r+模式下默认当前文件的指针
print(op.tell()) #关闭文件
op.close() #输出结果
#
# 文件内容----: 小狐狸
#
# #总结由此可以看出r+模式下默认文件指针指向文件开头
#在读取文件数据时指针被移动到文件最后
#由于读取文件后指针被移动到最后,所以内容相当于被追加到末尾,并将指针移动到最后

10.seek():移动文件的指针

fileObject.seek(offset[, whence])
说明:offset--开始的偏移量,也就是代表需要移动偏移的字节数
whence--可选,默认是0。给offset参数一个定义,表示要从哪个位置开始偏移;0代表从文件开头开始算起,
1代表从当前位置开始算起,2代表从文件末尾开始算起

项目实战

1.python中一个文件句柄问题

首先有2个文件file1.txt和file2.txt,接下来遍历两个文件的内容,如果内容一致则输出,否则不做处理

file1.txt:
11
22
33
44 file2.txt
11
33
44
55 示例代码:
#打开文件
opfie1 = open('file1.txt', 'r')
opfile2 = open('file2.txt', 'r') for i in opfie1.readlines():
for m in opfile2.readlines():
if i == m:
print(i)
else:
pass #关闭文件
opfie1.close()
opfile2.close()

大家猜猜会输出什么?   11 33还是11

答案是11    那是为什么呢?

那么接下来咱们调试一下代码,在for m in opfile2.readLines()之前打印一个opfile2.readLines()你就会发现问题

#1.打开文件
opfie1 = open('file1.txt', 'r')
opfile2 = open('file2.txt', 'r')
for i in opfie1.readlines():
print(opfile2.readlines())
for m in opfile2.readlines():
if i == m:
print(i)
else:
pass #关闭文件
opfie1.close()
opfile2.close() #输出结果
# # ['11\n', '33\n', '44\n', '55']
# []
# []
# []

细心的你会发现,只有第一次遍历才可以拿到file2.txt文件的所有内容,剩下的遍历并不会获取到file2.txt文件的所有内容,这是什么原因?这就是文件句柄,首先第一次循环结束后文件句柄就移动到文件最后了,接下来的循环并不会改变文件句柄的位置,所以只会输出11

#解决:解决这个问题有好多种解决办法,目前暂且写这一种
#打开文件
opfie1 = open('file1.txt', 'r')
opfile2 = open('file2.txt', 'r') ophandle1 = opfie1.readlines()
ophandle2 = opfile2.readlines() for i in ophandle1:
for m in ophandle2:
if i == m:
print(i)
else:
pass #关闭文件
opfie1.close()
opfile2.close() #输出
#
#

20180119-文件操作open用法的更多相关文章

  1. [python]打开文件操作open用法

    1. 格式 handle = open(file_name, access_mode = 'r') file_name: 希望打开的文件名 access_mode: 'r'表示读取,'w'表示写入,' ...

  2. 【python3之文件操作】

    一.文件操作 1.文件处理的流程 1)打开文件,得到文件句柄并赋值给一个变量 2)通过句柄对文件进行操作 3)关闭文件 例如: f = open('chenli.txt') #打开文件 first_l ...

  3. (二十三)Python 3 文件操作

    文件处理流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 打开文件的模式有三种: 只读模式(默认) 只写模式(不可读,不存在则创建,存在则覆盖) 追加模式(可读,不存在则 ...

  4. C语言对文件的操作函数用法详解2

    fopen(打开文件) 相关函数 open,fclose 表头文件 #include<stdio.h> 定义函数 FILE * fopen(const char * path,const  ...

  5. C语言对文件的操作函数用法详解1

    在ANSIC中,对文件的操作分为两种方式,即: 流式文件操作 I/O文件操作 一.流式文件操作 这种方式的文件操作有一个重要的结构FILE,FILE在stdio.h中定义如下: typedef str ...

  6. Python3中IO文件操作的常见用法

    首先创建一个文件操作对象: f = open(file, mode, encoding) file指定文件的路径,可以是绝对路径,也可以是相对路径 文件的常见mode: mode = “r”   # ...

  7. 强大的pdf文件操作小工具——PDFtk的小白用法 【转载】

    转载出处https://www.cnblogs.com/basterdaidai/p/6204518.html 前言 作为程序员,大家都知道的,总是会被技术小白问各种跟编程没什么关系的硬件.软件问题. ...

  8. Python之路第一课Day3--随堂笔记(文件操作)

    一.集合的介绍 1.集合操作 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 常用操作 s = se ...

  9. 003-Tuple、Array、Map与文件操作入门实战

    003-Tuple.Array.Map与文件操作入门实战 Tuple 各个元素可以类型不同 注意索引的方式 下标从1开始 灵活 Array 注意for循环的until用法 数组的索引方式 上面的for ...

  10. C的文件操作2

    [转] C语言文件操作  概述 所谓文件(file)一般指存储在外部介质上数据的集合,比如我们经常使用的mp3.mp4.txt.bmp.jpg.exe.rmvb等等.这些文件各有各的用途,我们通常将它 ...

随机推荐

  1. CF429E Points and Segments

    链接 CF429E Points and Segments 给定\(n\)条线段,然后给这些线段红蓝染色,求最后直线上上任意一个点被蓝色及红色线段覆盖次数之差的绝对值不大于\(1\),构造方案,\(n ...

  2. ubuntu idea 安装

    一.下载 1.进入官网 下载对应安装包 https://www.jetbrains.com/idea/download/#section=linux sudo wget https://downloa ...

  3. ZROI2019 提高十连测

    额 掰手指头一数 特么又是第三年十连测了= = 2017一场没打 那时候好像一场比赛也就100人左右 2018前几场还都好好补了 后来开始放飞自我了 这时候一场有150人还多了 2019想让今年的No ...

  4. 用C语言编程乘法口诀表

    首先是全部,代码如下: #include<stdio.h> void main() { int i,j; ;i<=;i++) { ;j<=;j++) printf(" ...

  5. 6,Stack

    一,Stack简介 Stack是栈.它的特性是:先进后出(FILO, First In Last Out). java工具包中的Stack是继承于Vector(矢量队列)的,由于Vector是通过数组 ...

  6. FTP错误 [ftp: connect: No route to host] 解决方法

    问题: 昨天在局域网内的两台机器上用ftp命令传文件.因为是新机器所以没安装ftp. 分别在两台机器上安装了ftp的服务端和客户端,并启动了ftp服务器进程. 当用启动ftp连接另一台机器时发生了如下 ...

  7. 【HDOJ6655】Just Repeat(贪心)

    题意:A和B两个人玩游戏,分别有n和m张牌,A的第i张牌是a[i],B是b[i] 两人轮流出牌,如果一种编号的牌被其中一个人出了另一个人就不能出自己手中这个编号的牌 两人都按最优策略行动,问获胜者 思 ...

  8. 20180803-Java 流(Stream)、文件(File)和IO

    Java 流(Stream).文件(File)和IO 下面的程序示范了用read()方法从控制台不断读取字符直到用户输入"q". // 使用BufferedReader 在控制台读 ...

  9. WINDOWS2008server安全策略设置

    一.防止黑客或恶意程序暴力破解我的系统密码 答: 暴力破解Windows密码实质上是通过穷举算法来实现,尤其是密码过于简单的系统,暴力破解的方法还是比较实用的.有一点需要我们注意,这个问题的关键在于W ...

  10. Windows7系统C盘空间不足

    C盘要满了,用WizTree发现:;两个大文件, ①睡眠有关的,用命令提示符(管理员身份运行), 命令窗口中输入 powercfg -h off,即可关闭休眠功能,同时 Hiberfil.sys 文件 ...