Python 根据音频长度批量重命名目录中的 .WAV 文件的脚本
Python script to batch-rename .WAV files in a directory, based on audio length
我正在编写一个 Python 脚本来重命名目录中的一堆 .WAV 文件,并重命名它们,将音频文件的长度添加到文件名的开头。
到目前为止我得到的是:
import wave
import contextlib
import os
for file_name in os.listdir(os.getcwd()):
if file_name.endswith(".wav") or file_name.endswith(".aiff"):
with contextlib.closing(wave.open(file_name, 'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
duration = int(duration)
duration = str(duration)
new_file_name = duration + " " + file_name
print(file_name)
os.rename(file_name, new_file_name)
else:
continue
但是我得到了这个错误:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'TEMFX01-RisingLong001.wav' -> '15 TEMFX01-RisingLong001.wav'
如何让进程停止使用它以便重命名它?
谢谢!
编辑:真的很傻,只需要在 print(file_name).
之前添加 f.close()
不确定我是应该删除这个主题还是回答我自己的问题?
重命名前关闭wave。
import wave
import contextlib
import os
for file_name in os.listdir(os.getcwd()):
if file_name.endswith(".wav") or file_name.endswith(".aiff"):
with contextlib.closing(wave.open(file_name, 'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
duration = int(duration)
duration = str(duration)
new_file_name = duration + " " + file_name
print(file_name)
wave.close()
os.rename(file_name, new_file_name)
else:
import wave
import contextlib
import os
count = 0
for file_name in os.listdir(os.getcwd()):
if file_name.endswith(".wav") or file_name.endswith(".aiff"):
count += 1
with contextlib.closing(wave.open(file_name, 'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
duration = int(duration)
duration = str(duration)
new_file_name = "audio" + str(count) + '.wav'
print(file_name)
os.rename(file_name, new_file_name)
else:
continue
此代码将解决问题
我正在编写一个 Python 脚本来重命名目录中的一堆 .WAV 文件,并重命名它们,将音频文件的长度添加到文件名的开头。
到目前为止我得到的是:
import wave
import contextlib
import os
for file_name in os.listdir(os.getcwd()):
if file_name.endswith(".wav") or file_name.endswith(".aiff"):
with contextlib.closing(wave.open(file_name, 'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
duration = int(duration)
duration = str(duration)
new_file_name = duration + " " + file_name
print(file_name)
os.rename(file_name, new_file_name)
else:
continue
但是我得到了这个错误:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'TEMFX01-RisingLong001.wav' -> '15 TEMFX01-RisingLong001.wav'
如何让进程停止使用它以便重命名它?
谢谢!
编辑:真的很傻,只需要在 print(file_name).
之前添加 f.close()不确定我是应该删除这个主题还是回答我自己的问题?
重命名前关闭wave。
import wave
import contextlib
import os
for file_name in os.listdir(os.getcwd()):
if file_name.endswith(".wav") or file_name.endswith(".aiff"):
with contextlib.closing(wave.open(file_name, 'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
duration = int(duration)
duration = str(duration)
new_file_name = duration + " " + file_name
print(file_name)
wave.close()
os.rename(file_name, new_file_name)
else:
import wave
import contextlib
import os
count = 0
for file_name in os.listdir(os.getcwd()):
if file_name.endswith(".wav") or file_name.endswith(".aiff"):
count += 1
with contextlib.closing(wave.open(file_name, 'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
duration = int(duration)
duration = str(duration)
new_file_name = "audio" + str(count) + '.wav'
print(file_name)
os.rename(file_name, new_file_name)
else:
continue
此代码将解决问题