Search code examples
pythonpyusb

accessing a USB drive in python for a beginner


level student doing my second year project in computing and have decide to try and create a utility that access the USB drives and return a percentage value of how much space the different files and folders take up of the USB in a GUI(code for GUI).

from tkinter import *
from tkinter.ttk import *
def select():
    sf = "value is %s" % var.get()
    root.title(sf)
    # optional
    color = var.get()
    root['bg'] = color
root = Tk()

# use width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (1000, 600, 10, 10))
root.title("USB scanner")
frame1=Frame(root)
frame1.pack(side=TOP, anchor=W)
var = StringVar(root)
# initial value

var.set('select drive')
choices = ['', '', '', '','', '']
option = OptionMenu(frame1, var, *choices)
option.pack(side=LEFT)
button = Button(frame1, text="Scan selected drive", command=select)
button.pack(side=LEFT, padx=30, pady=30)

root.mainloop()

i have installed pyUSB-1.0.0a2 and libopenusb-1.1.11 i keep getting the back end is not available error i have checked DLL. if i'm honest i don't know what i'm doing to much so any basic help is appreciated


Solution

  • The following program should help to get you pointed in the right direction:

    from tkinter import *
    from shutil import disk_usage
    
    class Sizer(Frame):
    
        @classmethod
        def main(cls):
            NoDefaultRoot()
            root = Tk()
            root.geometry('{}x{}'.format(200, 100))
            root.resizable(False, False)
            root.title('Drive Information')
            widget = cls(root)
            widget.grid(row=0, column=0, sticky=NSEW)
            root.mainloop()
    
        def __init__(self, master=None, cnf={}, **kw):
            super().__init__(master, cnf, **kw)
            self.selection = StringVar(self)
            self.menu = OptionMenu(self, self.selection, 'A:\\', 'B:\\', 'C:\\')
            self.menu.grid(row=0, column=0, sticky=NSEW)
            self.button = Button(self, text='Scan the drive', command=self.scan)
            self.button.grid(row=1, column=0, sticky=NSEW)
            self.display = Label(self)
            self.display.grid(row=2, column=0, sticky=NSEW)
    
        def scan(self):
            disk = self.selection.get()
            try:
                information = disk_usage(disk)
            except FileNotFoundError:
                self.display['text'] = 'Disk {!r} cound not be found'.format(disk)
            else:
                self.display['text'] = 'Used: {}\nFree: {}'.format(
                    information.used, information.free)
    
    if __name__ == '__main__':
        Sizer.main()