# 导入包,很重要的
import os
"""
# 创建文件夹及文件
os.mkdir("./test")
for i in range(1, 6):
os.mkdir("./test/test"+str(i))
fp = open("./test/test"+str(i)+".txt", 'w',encoding='utf-8')
"""
while True:
print("功能代码:\n1.添加前缀\t2.删除前缀\t3.清除bak fas vlx log 文件")
sn = input("请选择您的操作代码:")
profix = input("请输入前缀:")
path = input("请输入您的文件路径:")
# 获取路径下的所有文件夹及文件
file_list = os.listdir(path)
# print(file_list)
# 遍历所有的文件
for i in file_list:
path_flag = os.path.isfile(os.path.join(path, i))
if not path_flag: # 跳过目录
continue
# 判断执行的操作.并对非目录的文件进行操作
if sn == '1':
# pass # 添加前缀
os.rename(os.path.join(path, i), os.path.join(path, profix + i)) # os.path.join(path, profix + i) 连接路径
elif sn == '2':
# pass # 删除前缀
if i.startswith(profix):
os.rename(os.path.join(path, i), os.path.join(path, i[len(profix):]))
elif sn == '3':
#pass # 删除bak fas vlx文件
n = 0
for root, dirs, files in os.walk('./'):
for name in files:
if(name.endswith(".bak") or name.endswith(".fas") or name.endswith(".vlx")
or name.endswith(".log")):
n += 1
print(n)
os.remove(os.path.join(root, name))
else:
print("执行代码错误!!!")
else:
print("执行成功!!")
此程序,3,清除项,在程序运行后,输入指定目录后,提示执行成功,但是实际并未删除指定后缀的文件,求大神指教,小弟新人一个,再次先拜谢了