日常方便使用的Python腳本實現


目錄

文件批量重命名

bin文件合並

正文

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])

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM