【问题标题】:Python os.walk for unmounted drives without drive letterPython os.walk 用于没有驱动器号的未安装驱动器
【发布时间】:2020-06-13 22:22:28
【问题描述】:

好的,所以我编写了一个简单的 Python 代码,它比较了 2 个文件夹中的所有文件,并从其中一个文件夹中删除了重复项。我已经使用它几个星期了,它在我所有的硬盘上都运行良好。 但是当我连接手机时,os.walk 似乎根本找不到地址

电话:Blackberry Key2(安卓) 平台:Jupyter Notebook 问题:从 USB 连接的手机似乎缺少驱动器号(见屏幕截图)。不过我不知道那是什么意思。This PC screenshot 我正在输入 os.walk 命令在我的计算机中显示的路径,但它不会读取它

path2 = r"RG0005\BlackBerryBBF1006\Internal shared storage\Pictures"

我试过搜索论坛,但我想我不知道没有驱动器号的驱动器叫什么,找不到任何相关的东西

任何帮助表示赞赏!谢谢!

python 笔记本代码:

import os
import pandas as pd
import csv
from datetime import datetime

pd.set_option('display.max_colwidth', 700)

#COMPARE THIS
path1 = r"F:\1_BBK1_20190623_Img_Bckup\1_Pictures"

#DELETE FROM PATH
path2 = r"RG0005\BlackBerryBBF1006\Internal shared storage\Pictures"

f1 = [] #path
f2 = [] #path
a1 = [] #filename
a2 = [] #filename

for root, dirs, files in os.walk(path1):
    for file in files:
        if (file.endswith(".jpg") 
            or file.endswith(".png") 
            or file.endswith(".jpeg") 
            or file.endswith(".mov")  
            or file.endswith(".mp4") 
            or file.endswith(".pdf") 
            or file.endswith(".xlsx") 
            or file.endswith(".txt")):
            f1.append(os.path.join(root))
            a1.append(os.path.join(file))
ds1 = {"Path":f1,"filename":a1}
df1 = pd.DataFrame(ds1)

for root, dirs, files in os.walk(path2):
    for file in files:
        if (file.endswith(".jpg") 
            or file.endswith(".png") 
            or file.endswith(".jpeg") 
            or file.endswith(".mov")  
            or file.endswith(".mp4") 
            or file.endswith(".pdf") 
            or file.endswith(".xlsx") 
            or file.endswith(".txt")):
            f2.append(os.path.join(root))
            a2.append(os.path.join(file))
ds2 = {"Path":f2,"filename":a2}
df2 = pd.DataFrame(ds2)
# df2.head()

【问题讨论】:

    标签: python directory path filenames os.walk


    【解决方案1】:

    这样的 MTP 设备不是普通驱动器,您无法通过路径名访问它。

    您唯一的解决方案是使用 MTP 库连接到设备(或在手机上安装另一个文件服务器,例如 WebDav、FTP 或 SFTP 服务器并连接到该设备),或使用 COM 组件来执行此操作通过 Windows(但不是常规路径。这很难)。

    【讨论】:

    • 感谢您的快速回复。是的,我在过去 1 小时内对 MTP 进行了一些研究。以下是我发现的内容... 1. 使用 pymtp 2. 使用 ADB 3. 关于使用 Calibre 工具的源代码的一些事情(以上都提到了在这篇文章中stackoverflow.com/questions/11161747/…) 4. MTP 库 5. 手机上的文件服务器 对于每一个 1 - 没有太多可用的文档如何使用 2 - 如果你知道,有什么指南吗? 3 - 听起来很复杂 4 - 你能给我指一个 MTP 库吗? 5 - 我已经安装了 FTP 服务器 - 我该如何使用它?
    • 所以我要做的是使用我的脚本来识别重复项并发送删除命令......如果我可以通过 FTP 做到这一点,那对我来说会更好,因为我已经将此广泛用于两端的客户端和服务器设置。我需要在我的 python 代码中修改什么以通过 FTP 进行路径引用? ---------------- 另外-您谈到了Windows COM组件-这是否意味着在Linux中更容易执行-如果您能指出我的方向,我可以检查一下..
    【解决方案2】:

    电话上的 FTP 服务器选项适用于以下各项

    建立连接

    ############--- SFTP SETUP
    my_session_factory = ftputil.session.session_factory(
                           base_class=ftplib.FTP,
                           port=8888,
                           encrypt_data_channel=True,
                           debug_level=None)
    
    ############--- DEFINING VARIABLES
    f1 = [] #path
    f2 = [] #path
    a1 = [] #filename
    a2 = [] #filename
    pd.set_option('display.max_colwidth', 700)
    
    
    #---------------------#
    # PATH1 : COMPARE FROM (BASE FOLDER TO KEEP)
    #---------------------#
    # path1 = r"F:\1_BBK1_20190623_Img_Bckup\1_Pictures\To Organize\WhatsApp Images - moved to key2 for sorting"
    
    a_host = ftputil.FTPHost("192.168.0.xxx",'user','pwd',session_factory=my_session_factory)
    path1 = "/storage/emulated/0/Pictures/"
    a_host.chdir(path1)
    
    
    #---------------------#
    # PATH2 : COMPARE WITH (FOLDER TO DELETE FROM)
    #---------------------#
    # a_host = ftputil.FTPHost("192.168.1.xxx",'user','pwd',session_factory=my_session_factory)
    # path2 = "/storage/emulated/0/Pictures/"
    # a_host.chdir(path2)
    
    path2 = r"E:\2_SFTP Transfers Android\Screenshots"
    

    然后是查找常用文件并删除

    #---------------------#
    # CREATING DATAFRAMES
    #---------------------#
    for root, dirs, files in os.walk(path1):
        for file in files:
            if (file.endswith(".jpg") 
                or file.endswith(".png") 
                or file.endswith(".jpeg") 
                or file.endswith(".mov")  
                or file.endswith(".mp4") 
    #             or file.endswith(".pdf") 
    #             or file.endswith(".xlsx") 
                or file.endswith(".txt")):
                f1.append(os.path.join(root))
                a1.append(os.path.join(file))
    ds1 = {"Path":f1,"filename":a1}
    df1 = pd.DataFrame(ds1)
    
    for root, dirs, files in a_host.walk(path2):
        for file in files:
            if (file.endswith(".jpg") 
                or file.endswith(".png")
                or file.endswith(".jpeg") 
                or file.endswith(".mov")  
                or file.endswith(".mp4") 
    #             or file.endswith(".pdf") 
    #             or file.endswith(".xlsx") 
                or file.endswith(".txt")):
                f2.append(os.path.join(root))
                a2.append(os.path.join(file))
    ds2 = {"Path":f2,"filename":a2}
    df2 = pd.DataFrame(ds2)
    # df2.head(5)
    common = df1[df1.filename.isin(df2.filename)]
    common.shape
    # common.head()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-10
      • 2014-12-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 2018-08-28
      • 2014-06-23
      相关资源
      最近更新 更多