如何更改文件并仅将更改写入磁盘 - 基本上是 sed (python)?

发布于 2024-09-08 08:31:31 字数 192 浏览 2 评论 0原文

假设我有一个文件 /etc/conf1 ,

它的内容大致如下

option = banana
name = monkey
operation = eat

,假设我想用“鸵鸟”替换“猴子”。我怎样才能做到这一点而不将文件读入内存,更改它,然后将其全部写回?基本上,我如何“就地”修改文件?

Let's say I have a file /etc/conf1

it's contents are along the lines of

option = banana
name = monkey
operation = eat

and let's say I want to replace "monkey" with "ostrich". How can I do that without reading the file to memory, altering it and then just writing it all back? Basically, how can I modify the file "in place"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

葬心 2024-09-15 08:31:31

你不能。 “ostrich”比“monkey”多一个字母,因此您必须至少从那时起重写该文件。文件系统不支持向上或向下“移动”文件内容。

如果它只是一个小文件,则没有理由为此烦恼,您不妨重写整个文件。

如果它是一个非常大的文件,您将需要重新考虑文件内容的内部设计,例如使用基于块的方法。

You can't. "ostrich" is one letter more than "monkey", so you'll have to rewrite the file at least from that point onwards. File systems do not support "shifting" file contents upwards or downwards.

If it's just a small file, there's no reason to bother with even this, and you might as well rewrite the whole file.

If it's a really large file, you'll need to reconsider the internal design of the file's contents, for example, with a block-based approach.

萌逼全场 2024-09-15 08:31:31

您应该查看 fileinput 模块:

http://docs.python.org/library/fileinput。 html

有一个选项可以通过输入法执行就地编辑:

http ://docs.python.org/library/fileinput.html#fileinput.input

更新 - 示例代码:


import fileinput
import re
import sys

for line in fileinput.input(inplace=True):
    sys.stdout.write(re.sub(r'monkey', 'ostrich', line))

使用 sys.stdout.write 以免添加任何额外的换行符。

You should look at the fileinput module:

http://docs.python.org/library/fileinput.html

There's an option to perform inplace editing via the input method:

http://docs.python.org/library/fileinput.html#fileinput.input

UPDATE - example code:


import fileinput
import re
import sys

for line in fileinput.input(inplace=True):
    sys.stdout.write(re.sub(r'monkey', 'ostrich', line))

Using sys.stdout.write so as not to add any extra newlines in.

折戟 2024-09-15 08:31:31

这取决于你所说的“就地”是什么意思。如果你想用 supercalifragilisticexpialidocious 替换 monkey 该怎么办?您想覆盖剩余的文件吗?如果没有,您将必须提前阅读并将文件的后续内容向前移动。

It depends on what you mean by "in place". How can you do it if you want to replace monkey with supercalifragilisticexpialidocious? Do you want to overwrite the remaining file? If not, you are going to have to read ahead and shift subsequent contents of the file forwards.

烟若柳尘 2024-09-15 08:31:31

CPU 指令对来自内存的数据进行操作。

您希望读取的文件部分必须先驻留在内存中,然后才能读取;在将任何内容写入磁盘之前,该信息必须位于内存中。

整个文件不必立即存在,但要对整个文件进行搜索替换,文件的每个字符都会在某个时刻通过 RAM。

您可能正在寻找类似于 mmap() 系统调用的东西。上面的文件输入模块听起来似乎是一个可以使用的东西。

CPU instructions operate on data which come from memory.

The portion of the file you wish to read must be resident in memory before you can read it; before you write anything to disk, that information must be in memory.

The whole file doesn't have to be there at once, but to do a search-replace on an entire file, every character of the file will pass through RAM at some point.

What you're probably looking for is something like the mmap() system call. The above fileinput module sounds like a plausible thing to use.

我不吻晚风 2024-09-15 08:31:31

仅当您不更改文件大小或仅附加到文件时,就地修改才容易。以下示例将文件的第一个字节替换为“a”字符:

fd = os.open("...", os.O_WRONLY | os.O_CREAT)
os.write(fd, "a")
os.close(fd)

请注意,Python 的 file 对象不支持此操作,您必须使用低级函数。要追加,请在 "a" 模式下使用 open() 函数打开文件 file。

In-place modifications are only easy if you don't alter the size of the file or only append to it. The following example replaces the first byte of the file by an "a" character:

fd = os.open("...", os.O_WRONLY | os.O_CREAT)
os.write(fd, "a")
os.close(fd)

Note that Python's file objects don't support this, you have to use the low-level functions. For appending, open file file with the open() function in "a" mode.

一影成城 2024-09-15 08:31:31

sed -i.bak '/monkey$/newword/' 文件

sed -i.bak '/monkey$/newword/' file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文