- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile_delete.py
More file actions
Latest commit
33 lines (23 loc) · 690 Bytes
/
Copy pathFile_delete.py
File metadata and controls
33 lines (23 loc) · 690 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Python Delete File
# Delete a File
# To delete a file, you must import the OS module, and run its os.remove() function:
# Example
# Remove the file "demofile.txt":
importos
os.remove("demofile.txt")
# Check if File exist:
# To avoid getting an error, you might want to check if the file exists before you try to delete it:
# Example
# Check if file exists, then delete it:
importos
ifos.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
# Delete Folder
# To delete an entire folder, use the os.rmdir() method:
# Example
# Remove the folder "myfolder":
importos
os.rmdir("myfolder")
# *Note: You can only remove empty folders.