1

Ok so I have written a simple Python code that compares all the files in 2 folders and deletes the duplicates from one of them. I've been using it for a few weeks now and it all works fine on all my hard disks. But when I connected my phone, the os.walk does not seem to locate the address at all

Phone : Blackberry Key2 (android) Platform : Jupyter Notebook Issue : The phone when connected from USB appears to have missing a Drive Letter (see screenshot). I don't know what that means though.This PC screenshot I'm entering the path as it appears in my computer for the os.walk command but it won't read it

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

I've tried searching the forums but I guess I don't know what a drive without a drive letter is called and couldn't find anything relevant

Any help is appreciated!! Thanks!

Code for python notebook:

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()
user680522
  • 21
  • 4

2 Answers2

0

An MTP device such as this is not a normal drive and you cannot access it through path names.

Your only solutions are to use an MTP library to connect to the device (or install another file server on your phone such as a WebDav, FTP, or SFTP server and connect to that), or to use COM components to do this through Windows (but not regular paths. This is hard).

remram
  • 4,805
  • 1
  • 29
  • 42
  • Thank you for the quick response. Yes I was doing some research on MTP for past 1 hour.. Following are what I've found... 1. Use pymtp 2. Use ADB 3. Something about using source code from a tool called Calibre (all the above are mentioned in this post https://stackoverflow.com/questions/11161747/how-to-access-an-mtp-usb-device-with-python) 4. MTP Library 5. File Server on phone For each 1 - not much documentation available how to use 2 - any guide if you know? 3 - sounds complicated 4 - Can you point me to an MTP Library? 5 - I've got FTP servers installed - how can I use that? – user680522 Jun 13 '20 at 23:35
  • So what I'm trying to do is to use my script to identify duplicates and send the delete commands... If I can do that through FTP it will be even better for me because I've used this extensively with clients and servers setup on both ends. what do I need to modify in my python code for having path reference through FTP? ---------------- Also - you said something about Windows COM components - does this mean it is easier to do in Linux - if you can point me in the direction I can check it out.. – user680522 Jun 13 '20 at 23:39
0

FTP Server on Phone option works with the following

Establishing Connection

############--- 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"

And after this for finding common files and deleting

#---------------------#
# 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()
user680522
  • 21
  • 4