我试图让我的代码在插入闪存驱动器时检测到,然后继续执行代码。我目前使用“os.path.exists”。当我使用插入的闪存驱动器启动代码时,代码运行正常,但是,如果我在拔下闪存驱动器时启动并尝试在代码运行时将其插入,代码将不检查闪存驱动器是否已插入并继续强迫代码进入睡眠状态。我怎样才能让代码工作?等待目录(Windows)存在以继续执行代码? (2.7)
import os
import sys
import datetime
from datetime import datetime
import shutil
import time
#Wait for FlashDrive to be connected
if os.path.exists("F:\"):
connected = 1
else:
connected = 0
while connected == 0:
print "..."
time.sleep(10)
#Get current date
currentdate=datetime.now().strftime("%m-%d-%Y")
print "Photos saved: " + currentdate
#Copy and rename DCIM
src = "F:/Pictures"
dst = "C:/Users/Josh/Desktop/photos/" + currentdate
shutil.copytree(src, dst)
的代码应该是一个循环,并执行每一个iPhone连接,永不停止运行的时间,但我不能让代码,如果它并没有真正检查的闪存驱动器的工作。
请注意,写入的代码是'SyntaxError',因为路径末尾的反斜杠正在转义结尾引号。处理Windows路径时,请始终使用原始字符串,转义反斜杠或使用正斜杠而不是后向斜杠。 – abarnert
另一方面,不是使用'0'和'1',因为它们碰巧是虚假的,它使用'False'和'True'更清晰。而且,不是'connected == 0'(或者'connected == False'),它更具可读性,并且更加健壮,只需检查“未连接”。 – abarnert