如何更改文件并仅将更改写入磁盘 - 基本上是 sed (python)?
假设我有一个文件 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你不能。 “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.
您应该查看 fileinput 模块:
http://docs.python.org/library/fileinput。 html
有一个选项可以通过输入法执行就地编辑:
http ://docs.python.org/library/fileinput.html#fileinput.input
更新 - 示例代码:
使用 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:
Using sys.stdout.write so as not to add any extra newlines in.
这取决于你所说的“就地”是什么意思。如果你想用
supercalifragilisticexpialidocious
替换monkey
该怎么办?您想覆盖剩余的文件吗?如果没有,您将必须提前阅读并将文件的后续内容向前移动。It depends on what you mean by "in place". How can you do it if you want to replace
monkey
withsupercalifragilisticexpialidocious
? 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.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.
仅当您不更改文件大小或仅附加到文件时,就地修改才容易。以下示例将文件的第一个字节替换为“a”字符:
请注意,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:
Note that Python's
file
objects don't support this, you have to use the low-level functions. For appending, open file file with theopen()
function in"a"
mode.sed -i.bak '/monkey$/newword/' 文件
sed -i.bak '/monkey$/newword/' file