Python實現批量檔案修改字尾

2021-08-21 05:46:50 字數 1717 閱讀 3678

decode_file()函式將指定路徑檔案配批量修改字尾名,然後存放到另乙個指定位置;

recover_file()函式將修改過的檔名字尾批量還原;

import os

def decode_file():

src_path = input(">>>please input path need to decode:")

tgt_path = input(">>>please input path to release decode file:")

l =

for root, dirs, files in os.walk(src_path):

for file in files:

for srcfile in l:

tgtfile = tgt_path + srcfile[len(src_path) + 1:]

tgtfile = tgtfile.split(".")[0] + "." + tgtfile.split(".")[-1] + "decode"

try:

src_f = open(srcfile, "rb")

except ioerror:

print("###原始檔開啟錯誤,程式退出!!!")

src_f.close()

return

if not os.path.exists(os.path.dirname(tgtfile)):

os.makedirs(os.path.dirname(tgtfile))

try:

tgt_f = open(tgtfile, "wb")

except ioerror:

print("###目標檔案生產錯誤,程式退出!!!")

tgt_f.close()

return

tgt_f.write(src_f.read())

src_f.close()

tgt_f.close()

def recover_file():

src_path = input(">>>please input path need to recover:")

l =

for root, dirs, files in os.walk(src_path):

for file in files:

for srcfile in l:

os.rename(srcfile,srcfile.split(".")[0] + "." + srcfile.split(".")[-1].replace("decode",""))

if __name__ == "__main__":

while true:

func = input("---------------------------------------\nmenu\nchoose a function:\n1.decode file\n2.recover file\n3.exit\n---------------------------------------\n>>>")

if func == "1":

decode_file()

elif func == "2":

recover_file()

elif func == "3":

break

else:

print(">>>error,choose again!\n")

python 讀csv檔案 修改後儲存

1.建立乙個讀物件和寫物件 2.逐行讀取csv檔案中的內容,逐行追加到寫物件中 3.寫完後.close 關閉寫物件 4.最後執行mv 更好檔名 需在linux下 5.is number函式是用於判斷是否是數字,可不管 import csv import os def is number s try ...

python批量修改字尾名

例如 將a.txt.doc改為a.txt import os os.chdir dir name 絕對路徑 or 相對路徑 file list os.listdir 獲取當前目錄下的檔案列表 or os.listdir dir for i in file list if i.endswith txt...

Python解析xml檔案並修改後儲存 demo

前情提要 需要對底層的引數檔案 xml格式 進行讀取,並將前端下發的資料覆蓋原資料,並儲存。下發引數格式 json 呈現 from xml.dom import minidom targetpath os.path.join path,targetfile print targetpath 列印引數...