日常方便使用的Python脚本实现
目录
正文
1.python根据不同条件批量实现文件重命名
因为下载的电视剧名字比较乱,但却按照下载时间顺序依次排列,而手动重命名或者找软件太麻烦,我就自己实现了个:
import os import time def rename(path): Num = 1 #获得当前文件夹下的所有文件路径 pathlist = [os.path.join(path, filename) for filename in os.listdir() if filename.split('.')[-1] != 'py'] #根据文件修改时间或创建时间来进行排序 #time.ctime(os.path.getmtime(path))/time.ctime(os.path.getctime(path)) pathlist = sorted(pathlist, key=lambda path:' '.join(time.ctime(os.path.getmtime(path)).split(' ')[::-1]), reverse=False); #对排序后的文件进行重命名 for pathname in pathlist: newname = str(Num) + ". 漂亮的李慧珍" + ".mp4"; Num += 1; os.rename(pathname, os.path.join(path, newname)) print(pathname, newname); #获得当前文件夹路径 path = os.path.split(os.path.realpath(__file__))[0]; rename(path)
2.根据偏移值实现bin文件合并
#/usr/bin/python import os import sys from struct import * #bin文件合并 def bin_connect(bin1, bin2, outbin, offset): fin1 = open(bin1, 'rb') fin2 = open(bin2, 'rb') fout = open(outbin,'wb') result = fin1.read() i = len(result) while i<int(offset, 16): i+=1 result += b'\0' # a bytes-like object is required, not 'str', ''->b'' result += fin2.read() fout.write(result) fin1.close(); fin2.close(); fout.close(); #举例 #combine.py -i bootloader.bin ramdisk.bin 0x10000 -o combine.bin if len(sys.argv) != 7 or sys.argv[1] != '-i' or sys.argv[5] != '-o': print('usage:') print('convert binary format to hexadecimal format: ') print('combine.py -i startfile endfile offset -o outfile') exit(0) bin_connect(sys.argv[2], sys.argv[3], sys.argv[6], sys.argv[4])