python - 打开外部文件可能会产生错误或在读取模式下创建

标签 python file external ioerror

昨晚我编写了一个程序,将所有注释行从文件中提取出来,并将它们输出到一个新文件中。这是我们在编程课上经常要做的事情,通过文件复制和粘贴进行挑选很快就会过时。

我检查了输入文件和输出文件,看看它们是否存在。如果输入文件确实存在,一切正常继续 - 如果不存在,则要求另一个文件名。如果输出文件确实存在,询问用户是否要覆盖——如果不存在,一切顺利。

我遇到的问题是,当 file = open(fileName, 'r') 找不到文件时,一项检查会正确抛出 IOError,另一项检查会创建一个空白文件,而不是给出 IOError。

这尤其令人困惑,因为这两位代码几乎相同。相同的过程,只是不同的文件名变量...

代码如下。第二部分是创建一个空白文件。首先给出预期的错误。

# Try to open the input file
inFileOpen = False
while not inFileOpen and userTrying:
    # If it opens, all good
    try:
        inFile = open(inFileName, 'r')
        inFileOpen = True
    # If it doesn't open, ask user to try a different file
    except IOError:
        ...

# Try to open the output file
toFileOpen = False
while not toFileOpen and userTrying:
    # If the file opens in r mode, that means it exists already, so ask user if they
    # want to overwrite the existing file, if not ask for a new file name
    try:
        # ********************
        # For some reason, 'r' mode is creating a file... no clue...
        # ********************
        toFile = open(toFileName)
        toFile.close() # I have tried removing this just guessing at solutions. Didn't work.

        # ... Ask if user wants to overwrite

    # If the file can't be opened, all good, that means it doesn't exist yet
    except IOError:
        toFileOpen = False
        toFile = open(toFileName, 'w')

最佳答案

如果您使用的是 Python 3.3,open 具有“x”模式,与“w”相同,只不过如果文件打开,它会引发 FileExistsException已经在那里了。这比单独的检查更好,因为它是测试现有文件和打开文件进行写入之间的原子和免疫竞争条件。虽然这对于您当前的脚本来说可能不是问题,但对于安全性更高的程序可能很重要。

在旧版本的 Python(2.x 和 3.0-3.2)中,这有点棘手。一种选择是打开文件进行追加(“a”模式),然后检查当前流位置(使用 tell)以查看它是否已包含某些内容。如果是这样,但用户想要替换旧的内容,可以调用truncate(),然后调用seek(0)来删除当前数据并返回到文件的开头。这仍然可能会在没有通知的情况下覆盖零长度文件,但我怀疑这不是一个大问题。另一种方法是将 os.O_EXCL|os.O_CREAT 标志显式传递给 os.open,然后将返回的文件描述符传递给 os.fdopen > 获取常规 Python 文件对象。这本质上就是“x”模式在幕后所做的事情。

编辑:这是一个漂亮的上下文管理器解决方案,用于创建输出文件,并提供合适的 get_filenameconfirm_overwrite 函数,这些函数与用户。此代码适用于 Python 3.3,但可以修改 open 调用以及 except 语句以使其适用于早期版本。

from contextlib import contextmanager

@contextmanager
def create_output_file():
    while True:
        out_filename = get_filename()

        try:
            with open(out_filename, "x") as f:
                yield f
            break

        except FileExistsException:
            if confirm_overwrite(out_filename):
                with open(out_filename, "w") as f:
                    yield f
                break

将其与 with 语句一起使用:

with create_output_file() as f:
    f.write("whatever")

关于python - 打开外部文件可能会产生错误或在读取模式下创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13335638/

相关文章:

php - 在本地 JavaScript 文件中调用外部 PHP 文件?

html - 如何设置从数据库中检索到的 <a> 样式?

javascript - 在外部文件和目录结构中存储 jquery 代码?

java - 如何将文件保存在内存中并读取文件输出流?

c# - 如何使用命令行参数中的文件路径在 C# 中打开访问数据库?

python - 通过 python 和 sqlite3 访问替代方案

python - 按用法对单词排序

Python在类中传递回调函数

python - 减少对 - python

file - 使用scp递归复制dir时如何过滤文件?