-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path文件扩展名修改,txt改zip为例=txt_zip(拖拽,多个文件或文件夹).py
57 lines (51 loc) · 2.07 KB
/
文件扩展名修改,txt改zip为例=txt_zip(拖拽,多个文件或文件夹).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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# encoding:utf-8
# https://github.com/wangandi520/andyspythonscript
from pathlib import Path
import sys
def doChangeSuffix(filePath, afterSuffix):
# type(filePath): Path
newFileName = Path(filePath).parent.joinpath(Path(filePath).stem + afterSuffix)
if not newFileName.exists():
Path(filePath).rename(newFileName)
print(Path(filePath).name + ' -> ' + Path(filePath).stem + afterSuffix)
def main(inputPath):
# 任何旧扩展名都改成新扩展名 = True,否则 = False
changeAllSuffix = False
# 没有扩展名的文件添加新扩展名 = True,否则 = False
addSuffix = True
# 从文件名读取参数
theFileName = Path(inputPath[0]).stem
if ('=' in theFileName and '_' in theFileName):
tempValue = theFileName.split('=')[1]
tempValue = tempValue.split('(')[0]
# 原扩展名
oldSuffix = '.' + tempValue.split('_')[0]
# 新扩展名
newSuffix = '.' + tempValue.split('_')[1]
del inputPath[0]
for aPath in inputPath:
if Path.is_dir(Path(aPath)):
for file in Path(aPath).glob('*'):
if addSuffix and file.suffix == '':
doChangeSuffix(file, newSuffix)
if changeAllSuffix and Path.is_file(file):
doChangeSuffix(file, newSuffix)
if not changeAllSuffix and Path.is_file(file) and file.suffix == oldSuffix:
doChangeSuffix(file, newSuffix)
if Path.is_file(Path(aPath)):
if addSuffix and Path(aPath).suffix == '':
doChangeSuffix(aPath, newSuffix)
if changeAllSuffix:
doChangeSuffix(aPath, newSuffix)
if not changeAllSuffix and Path(aPath).suffix == oldSuffix:
doChangeSuffix(aPath, newSuffix)
print()
print('执行结束')
getInput = input('输入回车退出或右上角关闭: ')
print()
if __name__ == '__main__':
try:
if len(sys.argv) >= 2:
main(sys.argv)
except IndexError:
pass