读取,处理然后在python中写入文件的理想方式 [英] Ideal way to read, process then write a file in python

查看:107
本文介绍了读取,处理然后在python中写入文件的理想方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多文件,对于每个文件,我需要阅读文本内容,对文本进行一些处理,然后将文本写回(替换旧内容)。

我知道我可以先打开这些文件作为 rt 来阅读和处理内容,然后关闭并重新打开它们为 wt ,但显然这不是一个好方法。我可以只打开一次文件进行读写吗?如何?

There are a lot of files, for each of them I need to read the text content, do some processing of the text, then write the text back (replacing the old content).
I know I can first open the files as rt to read and process the content, and then close and reopen them as wt, but obviously this is not a good way. Can I just open a file once to read and write? How?

推荐答案

请参阅: http://docs.python.org/2/library/functions.html#open


最常用的模式值是'r'用于读取,'w'用于写入(如果文件已经存在则截断文件)和'a'用于追加(在某些Unix系统上意味着所有无论当前的搜索位置如何,写入追加到文件的末尾。如果省略mode,则默认为'r'。默认设置是使用文本模式,该模式可以在写入时将\ n字符转换为特定于平台的表示,并在读取时返回。因此,在打开二进制文件时,您应该将'b'附加到模式值以在二进制模式下打开文件,这将提高可移植性。 (附加'b'即使在不以不同方式处理二进制文件和文本文件的系统上也很有用,它可以作为文档。)请参阅下面的更多可能的模式值。

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), 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). If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.

模式'r +','w +'和'a +'打开文件进行更新(注意'w +'截断文件)。将b附加到模式以在二进制模式下打开文件,在区分二进制文件和文本文件的系统上;在没有这种区别的系统上,添加'b'没有效果。

Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file). Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the 'b' has no effect.

所以,你可以打开一个文件mode r + ,从中读取,截断,然后写入同一个文件对象。但你不应该这样做。

So, you can open a file in mode r+, read from it, truncate, then write to the same file object. But you shouldn't do that.

你应该以读模式打开文件,写入临时文件,然后 os.rename 用于覆盖原始文件的临时文件。这样,你的行为是原子的;如果在写入步骤期间出现问题(例如,它被中断),您最终不会丢失原始文件,并且只是部分写出了替换文本。

You should open the file in read mode, write to a temporary file, then os.rename the temporary file to overwrite the original file. This way, your actions are atomic; if something goes wrong during the write step (for example, it gets interrupted), you don't end up having lost the original file, and having only partially written out your replacement text.

这篇关于读取,处理然后在python中写入文件的理想方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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