【发布时间】:2019-02-26 04:40:57
【问题描述】:
我正在尝试使用 os.walk 和 os.Popen 自动运行程序并从中保存图像。代码在这里:
#Loop through all files in directory
for root, dir, files, in os.walk(filepath):
for file in files:
#Find File Path of Song (need to not hard code)
fp = filepath +'\\' + str(file)
print('Processing: ', fp)
#Doulbe Check we haven't added before
#Call Spek with that filepath
subprocess.Popen([r'C:\Users\spitf_000\Downloads\spek-0.8.2\Spek\spek.exe', fp])
#Implementing adaptive wait time based on file size
file_size = os.stat(fp).st_size
wait_time = (file_size/1000000)
time.sleep(wait_time)
#Setting SAFE pyautogui
#Can tune once the rest is stable
pyautogui.FAIL_SAFE = True
pyautogui.PAUSE = 0.6 #Need to figure out how to make this run faster, it seems that 0.5 is the least amount of pause time between inputs for 'safe' results
#Key Presses to Save each file
pyautogui.hotkey('ctrl', 's')
pyautogui.press('enter')
pyautogui.hotkey('alt', 'f4')
我遇到的问题是程序将循环遍历目录中 5000 首歌曲中的大约 400 首。然后它将停止,我找不到错误消息或任何东西。
【问题讨论】:
-
我已经回答了下面的直接问题。没有收到来自 Python 的错误消息令人惊讶,我可能会建议使用 print 语句更精细地检测代码,以查看是否可以找到执行的最后一行。您还可以尝试通过隔离 os.walk(删除打印行下方的所有代码)并查看 os.walk 是否按预期遍历所有文件来回答您自己的问题。