'Deleting and making files with os isn't working

For some reason whenever I use os.rmdir or os.removedirs and os.makedirs or os.mkdir it doesn't work. My code is

import os

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

os.rmdir(path)

and the output is

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'

My computer has only one \



Solution 1:[1]

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

Use os.remove(path)

os.rmdir(path) is used to delete an empty directory not a file

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ElieTheDeveloper