以'rt'和'wt'模式打开文件 [英] Open files in 'rt' and 'wt' modes

查看:96
本文介绍了以'rt'和'wt'模式打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里有几次,我看到人们使用 rt wt 模式读取和写入文件。

Several times here on SO I've seen people using rt and wt modes for reading and writing files.

例如:

with open('input.txt', 'rt') as input_file:
     with open('output.txt', 'wt') as output_file: 
         ...

我没有看到记录的模式a>,但是由于 open()不会发出错误 - 看起来像使用相当合法。

I don't see the modes documented, but since open() doesn't throw an error - looks like it's pretty much legal to use.

它是什么,使用 wt vs w rt之间有什么区别? vs r

What is it for and is there any difference between using wt vs w and rt vs r?

推荐答案

t 指的是文本模式。 r rt w 之间没有区别, wt 因为文本模式是默认的。

t refers to the text mode. There is no difference between r and rt or w and wt since text mode is the default.

记录 here

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)

这篇关于以'rt'和'wt'模式打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆