用Python写获取目录下的文件,删除文件
一、获取目录下的第一个文件
def getRefile():
file_1=os.path.split(os.path.split(os.path.realpath(__file__))[0])[0]
report=os.path.join(file_1,"report")
all_file=os.listdir(report)
for a in all_file:
#print(a)
return a
二、删除文件
def delLog():
file_1=os.path.split(os.path.split(os.path.realpath(__file__))[0])[0]
log=os.path.join(file_1,"log/1.log")
try:
os.path.exists(log)
size=os.path.getsize(log)
if size>=9999:
os.remove(log)
except:
print("文件不存在")
三、删除某个文件夹下的文件或文件夹
import os import shutil def del_file(filepath): """ 删除某一目录下的所有文件或文件夹 :param filepath: 路径 :return: """ del_list = os.listdir(filepath) for f in del_list: file_path = os.path.join(filepath, f) if os.path.isfile(file_path): os.remove(file_path) elif os.path.isdir(file_path): shutil.rmtree(file_path)
浙公网安备 33010602011771号