简体   繁体   中英

Eject Device/USB using Python 3 (Windows 10)

I have a program that does backup to my USB, but to make it easier, i wanted to make it automatically eject when its done.

(did 'path' so i don't expose personal files) Code:

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)

(I know there are other questions, but i think this is the only one with python usb ejecting)

yeah i figured it out myself. code:

import os

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

this took me alot of time

finally...

Btw it might only work on my pc lol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM