Implementation of Pycharm Generating Files and Deleting Files in Specified Directory
- 2021-09-05 00:24:43
- OfStack
In the process of developing software, we often encounter operations that need to generate files and delete files in the specified directory. The following demonstrates how to use python for such operations.
Generate file
import os
# Create an empty file in the specified directory
def mkdir():
path = 'C:\wan\wan'// Here is the address of the generated file
for i in range(5):
file_name = path + str(oct(i))
os.mkdir(file_name)
mkdir()
Delete a file
import os
# Remove empty files from files
def clean_dir():
for i in range(5):
path = "C:\wan"// Here is the address of the file you want to delete
filename = path + str(oct(i))
try:
os.removedirs(filename)
except FileNotFoundError:
pass
clean_dir()
When generating files, 1 must pay attention to the number of generated files, 1 is thousands of folders accidentally!