如果不存在,写模式是否创建一个新文件?

时间:2022-09-25 02:54:04

I'm trying to write to a file that does not already exist using a file context manager.

我正在尝试使用文件上下文管理器编写一个尚未存在的文件。

a=open ('C:/c.txt' , 'w')

The above does not succeed. How would I create a file for writing if it does already exist?

上述方法并不成功。如果一个文件已经存在,我该如何创建它呢?

4 个解决方案

#1


29  

Yes, 'w' is specified as creating a new file -- as the docs put it,

是的,“w”被指定为创建一个新文件——正如文档中所说,

'w' for writing (truncating the file if it already exists),

'w'表示写入(如果文件已经存在,则将其截断),

(clearly inferring it's allowed to not already exist). Please show the exact traceback, not just your own summary of it, as details matters -- e.g. if the actual path you're using is different, what's missing might be the drive, or some intermediate directory; or there might be permission problems.

(清楚地推断它是不存在的)。请显示准确的回溯,而不只是你自己的总结,因为细节很重要——例如,如果你使用的实际路径不同,可能缺少的是驱动器或某个中间目录;或者可能有权限问题。

#2


3  

You most probably are trying to write to a directory that doesn't exist or one that you don't have permission writing to.

您很可能正试图写入一个不存在的目录或一个您没有权限写入的目录。

If you want to write to C:\foo\bar\foobar.txt then make sure that you've got a C:\foo\bar\ that exists (and in case permissions work on Windows, make sure you've got the permission to write there).

如果你想写C:\foo\bar\foobar。txt然后确保你有一个C:\foo\bar\(如果权限在Windows上工作,请确保你有在那里写的权限)。

Now when you open the file in write mode, a file should be created.

现在,当您以写模式打开文件时,应该创建一个文件。

#3


3  

[Edited to reflect that the problem is likely not forward vs. back slash]

[经过编辑,反映出这个问题很可能不是向前的,而是反斜杠的]

If I understood correctly, you want the file to be automatically created for you, right?

如果我理解正确的话,您希望文件自动为您创建,对吗?

open in write mode does create the file for you. It would be more clear if you told us the exact error you're getting. It might be something like you not having permission to write in C:.

在写模式中打开确实为您创建文件。如果你告诉我们你得到的确切的误差,就会更清楚。可能是你没有写C:的权限。

I had previously suggested that it might be because of the forward slash, and indicated that the OP could try:

我之前说过可能是因为前面的斜杠,并表示OP可以尝试:

a = open(r'C:\c.txt', 'w')

Note the r before the file path, indicating raw mode (that is, the backslash won't be interpreted as special).

注意文件路径前的r,表示原始模式(也就是说,反斜杠不会被解释为特殊)。

However, as Brian Neal pointed out (as well as others, commenting elsewhere), that's likely not the reason for the error. I'm keeping it here simply for historical purposes.

然而,正如布莱恩•尼尔(Brian Neal)指出的(以及其他评论人士),这很可能不是这个错误的原因。我把它放在这里只是为了历史目的。

#4


0  

If you're asking how to be warned when the file doesn't exist, then you need to explicitly check for that.

如果您正在询问如何在文件不存在时得到警告,那么您需要显式地检查它。

See here

在这里看到的

#1


29  

Yes, 'w' is specified as creating a new file -- as the docs put it,

是的,“w”被指定为创建一个新文件——正如文档中所说,

'w' for writing (truncating the file if it already exists),

'w'表示写入(如果文件已经存在,则将其截断),

(clearly inferring it's allowed to not already exist). Please show the exact traceback, not just your own summary of it, as details matters -- e.g. if the actual path you're using is different, what's missing might be the drive, or some intermediate directory; or there might be permission problems.

(清楚地推断它是不存在的)。请显示准确的回溯,而不只是你自己的总结,因为细节很重要——例如,如果你使用的实际路径不同,可能缺少的是驱动器或某个中间目录;或者可能有权限问题。

#2


3  

You most probably are trying to write to a directory that doesn't exist or one that you don't have permission writing to.

您很可能正试图写入一个不存在的目录或一个您没有权限写入的目录。

If you want to write to C:\foo\bar\foobar.txt then make sure that you've got a C:\foo\bar\ that exists (and in case permissions work on Windows, make sure you've got the permission to write there).

如果你想写C:\foo\bar\foobar。txt然后确保你有一个C:\foo\bar\(如果权限在Windows上工作,请确保你有在那里写的权限)。

Now when you open the file in write mode, a file should be created.

现在,当您以写模式打开文件时,应该创建一个文件。

#3


3  

[Edited to reflect that the problem is likely not forward vs. back slash]

[经过编辑,反映出这个问题很可能不是向前的,而是反斜杠的]

If I understood correctly, you want the file to be automatically created for you, right?

如果我理解正确的话,您希望文件自动为您创建,对吗?

open in write mode does create the file for you. It would be more clear if you told us the exact error you're getting. It might be something like you not having permission to write in C:.

在写模式中打开确实为您创建文件。如果你告诉我们你得到的确切的误差,就会更清楚。可能是你没有写C:的权限。

I had previously suggested that it might be because of the forward slash, and indicated that the OP could try:

我之前说过可能是因为前面的斜杠,并表示OP可以尝试:

a = open(r'C:\c.txt', 'w')

Note the r before the file path, indicating raw mode (that is, the backslash won't be interpreted as special).

注意文件路径前的r,表示原始模式(也就是说,反斜杠不会被解释为特殊)。

However, as Brian Neal pointed out (as well as others, commenting elsewhere), that's likely not the reason for the error. I'm keeping it here simply for historical purposes.

然而,正如布莱恩•尼尔(Brian Neal)指出的(以及其他评论人士),这很可能不是这个错误的原因。我把它放在这里只是为了历史目的。

#4


0  

If you're asking how to be warned when the file doesn't exist, then you need to explicitly check for that.

如果您正在询问如何在文件不存在时得到警告,那么您需要显式地检查它。

See here

在这里看到的