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

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

问题描述

在这里好几次,我看到人们使用 rtwt 模式来读取和写入文件.

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: 
         ...

我没有看到模式记录,但是因为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.

它的用途是什么,使用 wtwrtr 之间有什么区别吗?

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

推荐答案

t 指的是文本模式.rrtwwt 之间没有区别,因为文本模式是默认的.

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

记录在此处:

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)

默认模式为'r'(打开阅读文本,'rt'的同义词).

The default mode is 'r' (open for reading text, synonym of 'rt').

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

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