我用python创建了一个脚本,我想运行多个音频(.wav
)文件。4。有重复的语句,例如os.system
,time.sleep
出现多次,是否可以通过只编写一次来多次运行迭代?
import os
import time
command = "cmd"
print (" Starting iteration")
print ("Clearing device logs and will start to collect New logs")
os.system (" adb logcat -c")
time.sleep(3)
os.system (r'start C:\Alexa\whatshould.wav')
time.sleep(10)
print ("Capturing logs")
os.system (" adb logcat -d -v threadtime >> what_should_i_wear_today.txt")
time.sleep(3)
os.system (r'start C:\Alexa\wakeword.wav')
time.sleep(4)
print ("Capturing logs")
os.system (" adb logcat -d -v threadtime >> wakeword.txt")
time.sleep(3)
os.system (r'start C:\Alexa\photo.wav')
time.sleep(10)
print ("Capturing logs")
os.system (" adb logcat -d -v threadtime >> photo_capture.txt")
time.sleep(3)
os.system (r'start C:\Alexa\video.wav')
time.sleep(10)
print ("Capturing logs")
os.system (" adb logcat -d -v threadtime >> Video_capture.txt")
time.sleep(3)
我已经在Windows中创建了它,我可以在Mac等其他平台上使用此脚本吗?
答案 0 :(得分:1)
假设要运行目录中的所有轨道(在您的情况下为C:/Alexa
,则可以使用os.listdir()
函数返回该目录中所有文件的列表。
import os
import time
command = "cmd"
print (" Starting iteration")
print ("Clearing device logs and will start to collect New logs")
os.system (" adb logcat -c")
time.sleep(3)
path = 'C:/Alexa/'
song_list = os.listdir(path)
song_list.sort()
for song in song_list:
time.sleep(10)
os.system (f'start {os.path.join(path, song)}')
time.sleep(3)
print ("Capturing logs")
os.system (" adb logcat -d -v threadtime >> what_should_i_wear_today.txt")