0

I am writing a python script that checks the files in a folder on my Android, and will copy .jpg files to a USB I have mounted on my RPi. (My Android is running as an SSH server).

I am able to connect to my Android and get a list of filenames. I'm stuck as to how to copy the file from the Android 'server' to my USB Drive on the Pi. (Again, via Python script).

Here's what I have so far:

import paramiko as pmko
import os
import shutil
import subprocess


def list_files(directory, filetype, ssh):
    """
    This will scan a directory for the filetype,
    which is passed as `.jpg`, or `.mp3`, etc. and return
    a list of those files.
    """
    print("listing all photos in file.")
    distantFiles = list()
    filePath = '/storage/emulated/0/' + directory
    filePattern = '"*' + filetype + '"'
    rawcommand = 'find {path} -name {pattern}'
    command = rawcommand.format(path=filePath, pattern=filePattern)

    stdin, stdout, stderr = ssh.exec_command(command)
    filelist = stdout.read().splitlines()
    for afile in filelist:
        (head, filename) = os.path.split(afile)
        distantFiles.append(filename)
    return distantFiles

def connect_to_phone():
    host = "192.168.0.105"
    port = 2222
    username = "logintest"
    password = "supersecretpassword"

    print("Starting connection")
    ssh = pmko.SSHClient()
    ssh.set_missing_host_key_policy(pmko.AutoAddPolicy())
    ssh.connect(host, port=port, username=username,
                password=password)
    sftp = ssh.open_sftp()
    print("Connected!")
    return sftp, ssh


def main():
    sftp, ssh = connect_to_phone()
    files = list_files("DCIM/camera", ".jpg", ssh)
    directory = '/storage/emulated/0/' + 'DCIM/camera/'
    for file in files:
        _file = file.decode('utf-8')
        print(_file)
        #shutil.copy2(directory + _file, '/media/pi/usb/')
        sftp.put(directory+_file, '/media/pi/usb')  #no such file or directory '/storage/emulated/0/DCIM/camera/[whatever].jpg 
        quit()


if __name__ == "__main__":
    main()

So when I run this, I expected the sftp.put() to put the file in the /media/pi/usb folder (see this Answer), which does exist on the Pi (I followed #3 on this site to set up the USB, but named it USB, not USB1 and I can't rename due to permissions error...) - see screenshot of RPI's directories here.

However, I get this error when I run the program:

FileNotFoundError: [Errno 2] No such file or directory: '/storage/emulated/0/DCIM/camera/[jpeg name].jpg'

I think that's because it's looking for that directory on the RPi, not the SSH Server. How would I link that folder to the SSH Server, or otherwise copy the file from the SSH Server to the RPi's USB?

(I'm not sure if this is better here on RPi.SE or should be on StackExchange. Please migrate if preferable.)

  • sftp.put ... suggests sending file to the server - I thought you were trying to get files from the server ... is there an sftp.get? yeah documentation helps ... put(localpath, remotepath, callback=None, confirm=True) vs get(remotepath, localpath, callback=None) – Jaromanda X Mar 15 at 5:45
  • @JaromandaX ......d'oh!! I will try that. – BruceWayne Mar 15 at 12:52
  • @JaromandaX whelp that was it. Thanks!! If you make an answer I'll mark it – BruceWayne Mar 16 at 0:06
  • @JaromandaX care to make that an answer? – Ghanima Mar 17 at 15:37
1

Per the comments, I just needed to use the sftp.get method, not .put.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.