-
Notifications
You must be signed in to change notification settings - Fork 6
/
文件夹的最后一个文件删除(拖拽,递归,多个文件夹).py
41 lines (37 loc) · 1.17 KB
/
文件夹的最后一个文件删除(拖拽,递归,多个文件夹).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# encoding:utf-8
# https://github.com/wangandi520/andyspythonscript
from pathlib import Path
import sys
def getLastFile(filePath):
# type(filePath): Path
allFilePath = []
for file in Path(filePath).glob('*'):
if Path.is_file(file):
allFilePath.append(file)
allFilePath.sort()
return allFilePath[-1]
def main(inputPath):
# type(inputPath): Path
allFilePath = []
allDir = []
# 读取所有被拖拽的文件夹和子文件夹
for aPath in inputPath[1:]:
if Path.is_dir(Path(aPath)):
allDir.append(Path(aPath))
for file in Path(aPath).glob('**/*'):
if Path.is_dir(file):
allDir.append(file)
for eachDir in allDir:
allFilePath.append(getLastFile(eachDir))
for eachFile in allFilePath:
print(eachFile)
myChoice = input('要删除以上' + str(len(allFilePath)) + '个文件, 请输入y或yes:')
if myChoice.lower() in ['y', 'yes']:
for eachFile in allFilePath:
eachFile.unlink()
if __name__ == '__main__':
try:
if len(sys.argv) >= 2:
main(sys.argv)
except IndexError:
pass