【问题标题】:Python open() raises an error if I try to create a file如果我尝试创建文件,Python open() 会引发错误
【发布时间】:2011-10-15 14:01:01
【问题描述】:

我在 Windows 上遇到 Python 2.6 的问题。
我是否尝试:

fileobj=open("nonexistent.txt","w")

fil=os.open("nonexistent.txt", os.O_CREAT)
fileobj=file(fil)

我收到一个错误:

IOError: [Errno 2] 没有这样的文件或目录:'nonexistent.txt'

可能是什么问题?

【问题讨论】:

  • 如果您提供有关您的问题的误导性信息,您如何期望得到有用的答案,即。 e.如果您不提供触发错误的实际代码?

标签: python file io


【解决方案1】:

如果文件不存在,您将无法读取该文件,但是,您应该可以对其进行写入。下面的代码是否返回相同的错误?

f=open("nonexistent.txt","w")
f.write('Test')
f.close()

【讨论】:

  • 你也可以使用print >>f, 'Test'
【解决方案2】:

似乎是权限问题。 尝试在其他地方打开文件或以管理员身份运行 python。

【讨论】:

    猜你喜欢
    • 2022-11-04
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 2022-08-02
    • 2020-06-28
    • 2022-01-21
    • 1970-01-01
    • 2022-07-17
    相关资源
    最近更新 更多