python创建一个简单的文本文件失败

with open("D:\\test.txt", "w") as f:
    f.write("Hello, World!")

报错为:

OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\CHENHao\Python\Project\test.py", line 1, in
    with open("D:\test.txt", "w") as f:
OSError: [Errno 9] Bad file descriptor

代码本身应该没错吧?各位大拿帮我看下到底什么原因呢?

错误的文件描述符

报错中是\代码中是\\要么你代码没保存导致运行的与你写的不一致,要么你是打错了。建议是用/,不要用\这种有歧义的东西

分析思路: 代码本身没有问题(已验证).
坏的文件句柄
引发的原因是,你的文件本身已经打开(使用中),导致代码运行时,没有拿到执行和修改的权限,导致失败.