尽管“with”失败,仍对文件内容进行操作堵塞

发布于 2024-09-24 02:52:30 字数 884 浏览 0 评论 0原文

我刚刚用 Python 编写了一个实用程序来执行我需要的操作(无关紧要,但它是为内部 DSL 生成与 ctags 兼容的标记文件)。

无论如何 - 我在 with 语句的上下文中打开并读取文件,我很好奇,人们倾向于如何处理该过程中的失败?

我的解决方案是

with open(filename, 'rt') as f:
    content = f.read()

matches = re.findall(REGEX, content)

if len(matches) > 0:
    # do more stuff...
    pass

我将匹配检查放在 with 语句之外,因为我喜欢关闭文件并完成处理。但是,如果 content 从未构建,则此操作将会失败。

我的解决方案是将 content 初始化为这段代码上方的空字符串,但我的感觉是我希望该函数结束;函数或其他东西抛出异常。

在这种情况下,我可以将函数的其余部分放入 with 块中,但这会扩大打开文件的范围。我可以在 with 块之前创建 content ,以便它在出现故障时存在。然而,我很好奇,人们喜欢看到哪些其他解决方案(假设这个问题首先有意义)?

我想我有点像这样:

with open(filename, 'rt') as f:
    content = f.read()
else:
    content = ''

matches = re.findall(REGEX, content)

我会接受这样的想法:我只需要处理它,并让文件对其余功能保持打开状态(如果这是普遍共识)。 :)

I've just written a utility in Python to do something I need (irrelevant, but it's to generate a ctags-compatible tag file for an in-house DSL).

Anyway- I'm opening and reading the file in the context of a with statement, and I'm curious, how do people tend to handle failures in that process?

My solution is

with open(filename, 'rt') as f:
    content = f.read()

matches = re.findall(REGEX, content)

if len(matches) > 0:
    # do more stuff...
    pass

I put the match check outside of the with statement because I like having the file closed and done with. However, if content never gets built, this will fail.

My solution was to initialize content to the empty string just above this bit of code, but the feeling I get is that I'd like the function just to end; an exception gets thrown out of the function or something.

In this case, I could put the rest of the function into the with block but that broadens the scope of the open file. I can create content before the with block so that it exists in light of a failure. I'm curious, however, what other solutions do people like to see (assuming the question makes any sense in the first place)?

I suppose I'd sorta like something like this:

with open(filename, 'rt') as f:
    content = f.read()
else:
    content = ''

matches = re.findall(REGEX, content)

I will accept the idea that I just need to deal with it and leave the file open for the rest of the function if that's the general consensus. :)

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

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

发布评论

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

评论(1

忆沫 2024-10-01 02:52:30

我会做的就像你说的:

content = ''
with open(filename, 'rt') as f:
    content = f.read()

matches = re.findall(REGEX, content)

因为对于空字符串来说,正则化和检查匹配的成本可以忽略不计。

但是,只要最终关闭文件(假设您不重复使用它),立即关闭文件并不那么重要。

What I would do is as you said:

content = ''
with open(filename, 'rt') as f:
    content = f.read()

matches = re.findall(REGEX, content)

as the cost for regexing and checking matches would be negligable for an empty string.

However, closing the file immediately isn't that important as long as it is closed in the end, assuming that you don't reuse it.

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