使用 Python 3 (Windows 10) 弹出 Device/USB

Eject Device/USB using Python 3 (Windows 10)

我有一个程序可以备份到我的 USB,但为了方便起见,我想让它在完成后自动弹出。

(做了 'path' 所以我不暴露个人文件) 代码:

from shutil import copytree as ct
from shutil import copyfile as cf
from shutil import make_archive as arc
from shutil import rmtree as rm
from time import sleep as wait

f_drive = r'F:\BACKUP\stuff'
path_to_user = r'path'
another_path = r"path"
a_path = r'path'

def backup():
    ct(r'path', r'path')
    ct(r'path', r"F:\BACKUP\stuff\path")
    ct(r'path', r"path")
    ct(r'D:\path', r'path')

print("Starting backup...")
print("This process is automatic. This should not take any longer than 2 minutes...")
backup()
print()
print("Backup complete!")
print("Archiving folder...")
arc(r"F:\BACKUP", "zip", r'F:\BACKUP')
rm(r'F:\BACKUP')
print("Program will close in 10 seconds.")
wait(10)

(我知道还有其他问题,但我认为这是唯一一个 python USB 弹出)

是的,我自己想出来了。 代码:

import os

os.system('powershell $driveEject = New-Object -comObject Shell.Application; $driveEject.Namespace(17).ParseName("""F:""").InvokeVerb("""Eject""")')

这花了我很多时间

终于...

顺便说一句,它可能只适用于我的电脑哈哈