我没有经常使用python,但是我想创建一个在后台运行的连续脚本,并寻找插入的新驱动器。我希望脚本能够占用所有的来自驱动器的文件并将它们存储在我指定的另一个位置。这是某人可以帮助我的东西还是我应该在其他地方寻找?谢谢!
答案 0 :(得分:0)
这可能会让你开始:
import subprocess
import time
def get_drives():
"""Return a list of disk drives"""
output = subprocess.check_output("fsutil fsinfo drives")
return output[output.find(b":")+1:].decode("utf-8").split()
sleep_interval = 60 # seconds
while True:
# get a list of the current drives
drives = get_drives()
try:
time.sleep(sleep_interval)
# check for new drives
new_drives = get_drives()
for drive in new_drives:
if drive not in drives:
print(drive, "detected")
# do more stuff here
# update stored drives so we can detect removal & reinsertion
drives = new_drives
except KeyboardInterrupt:
print("Exiting")
break
对于您的测试,您可能希望将睡眠间隔设置为大约一秒左右。
按Ctrl+C
退出。
示例运行:
J:\>python drive_check.py
D:\ detected
Exiting