Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )
Copyright, without permission, is prohibited reprint
chapter
- Python Introduction
- Python development environment to build
- Python syntax
- Python variables
- Python Value Type
- Python type conversion
- Python String (String)
- Operators Python
- Python list (list)
- Python tuples (Tuple)
- Python collection (Set)
- Python dictionary (Dictionary)
- Python If … Else
- Python While loop
- Python For loop
- Python functions
- Python Lambda
- Python Classes and Objects
- Python inheritance
- Python iterator (Iterator)
- Python module
- Python date (Datetime)
- Python JSON
- Python regular expression (RegEx)
- Python PIP package manager
- Python exception handling (Try ... Except)
- Python Open File (File Open)
- Python read file
- Python write file
- Python delete files and folders
Delete Files
To delete a file, you need to import OS module, use one of the os.remove()
function:
Examples
Delete the file "test.txt":
import os
os.remove("test.txt")
Check if the file exists
To avoid errors, you need to check whether a file exists before deleting:
Examples
Delete the file exists before the examination:
import os
if os.path.exists("test.txt"):
os.remove("test.txt")
else:
print("文件不存在")
Delete the folder
To delete the entire folder, use the os.rmdir()
method:
Examples
Delete the folder "testfolder":
import os
os.rmdir("testfolder")
Note : You can only delete empty folders.