当前位置: 动力学知识库 > 问答 > 编程问答 >

Python open() raises an error if I try to create a file

问题描述:

I'm having a problem with Python 2.6 on windows.

Whether I try:

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

or

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

fileobj=file(fil)

I get an error:

IOError: [Errno 2] No such file or directory: 'nonexistent.txt'

What may be the problem?

网友答案:

You will not be able to read the file if it does not exist, however, you should be able to write to it. Does the code below return the same error?

f=open("nonexistent.txt","w")
f.write('Test')
f.close()
网友答案:

At seems like a permission problem. Try opening the file somewhere else or run the python as an Administrator.

分享给朋友:
您可能感兴趣的文章:
随机阅读: