使用 os 删除和制作文件无效

Deleting and making files with os isn't working

出于某种原因,每当我使用 os.rmdir 或 os.removedirs 和 os.makedirs 或 os.mkdir 它不起作用。我的密码是

import os

path = r"C:\Users\User\Desktop\Test\New Text Document.txt"

os.rmdir(path)

输出为

Traceback (most recent call last):
  File "C:\Users\User\Desktop\Python\projects\projects in python files\security\main.py", line 5, in <module>
    os.rmdir(path)
NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\Users\User\Desktop\Test\New Text Document.txt'

我的电脑只有一个\

import os
path = r"C:\Users\User\Desktop\Test\New Text Document.txt"
os.remove(path) 

使用os.remove(路径)

os.rmdir(路径)用于删除空目录而不是文件