簡體   English   中英

如何在 Python 中從 USB 連接的手機中讀取文件?

[英]How to read files from USB connected phone in Python?

我制作了一個 Python 程序,它讀取輸入路徑的目錄中的所有文件,然后對它們執行一些操作並將一些內容輸出到 .txt 文件中。

我使用 os.walk 讀取目錄中的文件

def dir_read(pathName = str(), fileList=[]):  #fileList is output
    cur_dir = [i[2] for i in os.walk(pathName)] #current directory
    for i in cur_dir:
        for j in i:
            fileList.append(j)

然后像這樣檢查正確輸入的路徑

fileList=[]

while True:
    dir_read(pathName, fileList) #FUNCTION CALL

    if not fileList:  #if it is empty
      print("The path is written incorrectly, is empty or does not exist. Please re-enter now or "
              "close the program and enter it inside PATH.txt:")
      pathName = str(input())
    else:
        break    #if the list is not empty (the path is entered correctly) the while loop breaks

但是,如果我輸入類似This PC\Nokia 6.1\Card SD之類的內容,它會不斷給我錯誤消息。 我還注意到桌面或文檔之類的東西也會發生同樣的事情。 但是,如果我輸入C:\Program Files (x86)\Notepad++之類的內容,它會完美運行。

我也嘗試用/替換\ ,但它不起作用。

我的手機沒有像 F 這樣的特定驅動器:如您在此處看到的: https ://imgur.com/a/Educ5RJ

我正在使用 Windows 10。

我該如何解決?

我相信你的目錄應該是這樣的:

在 Linux 上:

media/This PC/Nokia 6.1/Card SD

在 Windows 上:您需要知道可以運行此命令的驅動器號:

C:\>wmic logicaldisk where drivetype=2 get deviceid, volumename, description

假設它返回 X: 那么你的目錄應該是這樣的:

X:\Nokia 6.1\Card SD

我不確定您是否應該刪除諾基亞 6.1

嘗試使用subst命令,如果您的設備名稱上有空格,請將其放在引號中 例如: subst E: "The Name Goes Here"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM