使用 ConfigParser 替换已存在的字符串时出现问题

发布于 2024-09-17 06:49:33 字数 617 浏览 7 评论 0原文

我使用 ConfigParser 将简单设置保存到 .ini 文件中,其中一个设置是目录。每当我用较短的目录字符串(例如 D:/)替换目录字符串(例如 D:/Documents/Data)时,剩余的字符将在选项下放置两行。所以 .ini 文件现在看起来像这样:

[Settings]
directory = D:/

Documents/Data

我做错了什么?这是我的代码:

import ConfigParser

class Settings():
    self.config = ConfigParser.ConfigParser()

    def SetDirectory(self, dir): #dir is the directory string
        self.config.readfp(open('settings.ini'))
        self.config.set('Settings', 'directory', dir)
        with open('settings.ini', 'r+') as configfile: self.config.write(configfile)

I am using ConfigParser to save simple settings to a .ini file, and one of these settings is a directory. Whenever I replace a directory string such as D:/Documents/Data, with a shorter directory string such as D:/, the remaining characters are placed two lines under the option. So the .ini file now looks like this:

[Settings]
directory = D:/

Documents/Data

What am I doing wrong? Here is my code:

import ConfigParser

class Settings():
    self.config = ConfigParser.ConfigParser()

    def SetDirectory(self, dir): #dir is the directory string
        self.config.readfp(open('settings.ini'))
        self.config.set('Settings', 'directory', dir)
        with open('settings.ini', 'r+') as configfile: self.config.write(configfile)

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

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

发布评论

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

评论(1

凡间太子 2024-09-24 06:49:33

r+ 选项(在 with 中的 open 中)告诉 Python 保留文件以前的内容,只是覆盖将要删除的特定字节。写入它,但保留所有其他内容。使用 w 打开文件进行完全覆盖,这似乎是您应该在这里做的事情。您很少会想要覆盖现有文件中选定的字节,特别是对于文本文件,您更可能希望将其视为文本行序列,而不是一堆文本字节! (它在非常特殊的情况下很有用,主要涉及大型二进制文件,其中按字节视图可能有意义)。

我们用来查看文本文件的“按行组织”并没有反映在底层文件系统中(至少在当前流行的任何操作系统上 - 早在黑暗的过去一些文件组织中)例如,旨在模仿打孔卡包,因此每行必须恰好为 80 字节,不能多也不能少……但这对于绝大多数计算机程序员来说至多是一个遥远的古老记忆,今天的用户;-)。

因此,“就地覆盖文件的一部分”(文件包含不同长度的文本行)成为一个很大的问题。顺便说一句,如果您需要这样做,请考虑标准 Python 库的 fileinput 模块,它非常有效地模仿了这种经常需要的但针对文件系统的粒度运行的操作。但是,在这种情况下,它不会对您有太大帮助,因为简单的完全覆盖似乎是完全正确的;-)。

The r+ option (in the open in the with) is telling Python to keep the file's previous contents, just overwriting the specific bytes that will be written to it but leaving all others alone. Use w to open a file for complete overwriting, which seems to be what you should be doing here. Overwriting just selected bytes inside an existing file is very rarely what you want to do, particularly for text files, which you're more likely to want to see as sequence of lines of text, rather than bunches of bytes! (It can be useful in very specialized cases, mostly involving large binary files, where the by-byte view may make some sense).

The "by-line organization" with which we like to view text files is not reflected in the underlying filesystem (on any OS that is currently popular, at least -- back in the dark past some file organizations were meant to mimic packs of punched cards, for example, so each line had to be exactly 80 bytes, no more, no less... but that's a far-off ancient memory, at most, for the vast majority of computer programmers and users today;-).

So, "overwriting part of a file in-place" (where the file contains text lines of different lengths) becomes quite a problem. Should you ever need to do that, btw, consider the fileinput module of the standard Python library, which mimics this often-desired but-running-against-the-filesystem's-grain operation quite competently. But, it wouldn't help you much in this case, where simple total overwriting seems to be exactly right;-).

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