程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python deletes all files under the folder

編輯:Python
# -*- coding:utf-8 -*-
"""
author :sunli
date :2022 year 06 month 22 Japan 14:04
"""
# Delete all files under the folder ( Delete files only , Do not delete folder )
import os
path_data = r"D:\shujuji\ssss"
# python How to delete files os.remove(path)path Refers to the absolute path of the file , Such as :
# os.remove(r"E:\code\practice\data\1.py")# Delete file
# os.rmdir(r"E:\code\practice\data\2")# Delete folder ( Only empty folders can be deleted )
# shutil.rmtree(r"E:\code\practice\data\2")# Delete folder
# path_data = "E:\code\practice\data"#
def del_file(path_data):
for i in os.listdir(path_data): # os.listdir(path_data)# Return a list , Inside is the relative path of all things under the current directory
file_data = path_data + "\\" + i # Absolute path to everything under the current folder
if os.path.isfile(file_data) == True: # os.path.isfile Determine if it is a file , If it's a file , Just delete . If it's a folder . Recursive give del_file.
os.remove(file_data)
else:
del_file(file_data)
del_file(path_data)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved