這是我的代碼,但它缺少計算檔案的部分,仍然嘗試使用startswith()而不是正則運算式,也許在這種情況下它是一個更直接的解決方案
from pathlib import Path
def process_audio_files(file_name):
#Edit the file
transcription = file_name "edited"
return transcription
audio_chunks_path = "audio_chunks/"
names_structure = "chunk_"
#initializes the counter variable assuming that within the directory there may not be any files beginning with that word
n = 0
transcription = ""
if(audio_file.startswith(names_structure)): n = 1
#once all the files have been accounted for
for i in n:
file_name = audio_chunks_path names_structure str(i) ".mp3"
transcription = process_audio_files(file_name)
print(transcription)
Path(file_name).unlink() #delete this chunk audio file
為了使這個for回圈作業,我必須知道 n 的值是多少,為此我必須知道該目錄中有多少檔案,這就是問題所在。
在目錄中以該單詞開頭的檔案數量未知,因此變數的值n也將是未知的
audio_chunks/chunk_0.mp3
audio_chunks/chunk_1.mp3
audio_chunks/chunk_2.mp3
...
audio_chunks/chunk_n.mp3
uj5u.com熱心網友回復:
如果要計算檔案數,可以執行以下操作:
n = sum(1 for _ in Path("audio_chunks").glob("chunk_*.mp3"))
但是,如果您想查找檔案并洗掉它們,您可以使用相同的 glob 函式并執行以下操作:
for chunk_file in Path("audio_chunks").glob("chunk_*.mp3"):
transcription = process_audio_files(chunk_file)
print(transcription)
chunk_file.unlink()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/510291.html
