获取“IOError:[Errno 2]没有这样的文件或目录:“myoutfile.csv” ” Python 中的错误
我正在使用这一行创建一个新文件(该文件不存在):
with open(outfilename, 'rwb') as outfile:
它收到此错误:
IOError: [Errno 2] No such file or directory: 'myoutfile.csv'
我正在尝试创建此文件,我想如果我使用“w”,如果不存在,就会创建它存在。如果是权限,如何新建文件夹并引用其路径?
I am using this line to create a new file (the file does not exist):
with open(outfilename, 'rwb') as outfile:
And it gets this error:
IOError: [Errno 2] No such file or directory: 'myoutfile.csv'
I am trying to create this file, and I thought if I used 'w' that would create it if it doesn't exist. If it is permissions, how do I create a new folder and refer to its path?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
传递给
open()
函数的打开模式仅接受少数特定的字母组合。在你的例子中,'rwb'
不是这些组合之一,Python可能假设你的意思是'rb'
。尝试:这将打开文件进行写入。如果您需要既写入并从同一个句柄读取,请使用:
The open mode passed to the
open()
function accepts only a specific few combinations of letters. In your case,'rwb'
is not one of those combinations, and Python is perhaps assuming that you meant'rb'
. Try:This opens the file for writing. If you need to both write to and read from the same handle, use:
我非常确定
rwb
不是open
的有效模式。根据所需的行为,您可能必须选择r+b
或w+b
之一。如果您想读取现有文件,请使用
rb
。如果您想读取/写入现有文件,请使用
r+b
。使用
wb
是您想要写入现有或不存在的文件(将首先截断现有文件)。使用
w+b
是您想要读/写现有或不存在的文件(将首先截断现有文件)。如果您不想截断现有文件,请使用组合,例如(显然是伪代码):
I'm pretty certain
rwb
isn't a valid mode foropen
. Depending on the behaviour desired, you may have to opt for one ofr+b
orw+b
.Use
rb
is you want to read an existing file.Use
r+b
is you want to read/write an existing file.Use
wb
is you want to write an existing or non-existing file (will truncate an existing file first).Use
w+b
is you want to read/write an existing or non-existing file (will truncate an existing file first).Use a combination if you don't want truncation of an existing file, something like (pseudo-code, obviously):