2

I need to read a text file stored on a USB stick. PyUSB is what I need to search the USB device, but I don't understand how can I access the file after the device is mounted (on Linux and on Windows).

An idea please?

2 Answers 2

2

Just use the absolute path of the file (e.g. C:\text.txt).

To loop through the lines of a text file, use

for line in open("C:\text.txt", "rU"):
    #do stuff
Sign up to request clarification or add additional context in comments.

1 Comment

Best to use either open(r"C:\text.txt"...) or open("C:\\text.txt"...) - although '/' on Windows still works...
1

In Ubuntu the pendrive was under /media so I used:

import os
from os.path import join, getsize
for root, dirs, files in os.walk('/media'):
    print root, "consumes",
    print sum(getsize(join(root, name)) for name in files),
    print "bytes in", len(files), "non-directory files"

from http://docs.python.org/library/os.html#os.walk

in windows you will have to try with different drive letters maybe?

No need for PyUSB.

2 Comments

Hmm, my question is not good. I know how to open a file, but not how to find where the usb stick is mounted after it was plugged in.
Use os.path with try/except to open the folder of the pendrive. Maybe with a list of probable names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.