Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python automated script file transferring

Tags:

python

android

I am looking for a solution for copying all the files from a specific directory on the hard drive, to a specific or non specific directory on my android phone, once this device is connected.

I would like these files to be automatically moved (or at least copied) to my phone once I connect it to the computer and run the .py file.

I have windows 7 and python 2.7

I was trying this from another answer but I can't understand because there is few explanation, therefore I cannot get it to work.

edit: I have figured out how to transfer files between to folders but I want to my phone. So how can I fix the error of my system not finding the path of my phone, that'll fix my problem I believe. The code works fine the problem is the path.

Here is my code:

import os
import shutil
sourcePath = r'C:\Users\...\What_to_copy_to_phone'
destPath = r'Computer\XT1032\Internal storage'
for root, dirs, files in os.walk(sourcePath):

#figure out where we're going
dest = destPath + root.replace(sourcePath, '')

#if we're in a directory that doesn't exist in the destination folder
#then create a new folder
if not os.path.isdir(dest):
    os.mkdir(dest)
    print 'Directory created at: ' + dest

#loop through all files in the directory
for f in files:

    #compute current (old) & new file locations
    oldLoc = root + '\\' + f
    newLoc = dest + '\\' + f

    if not os.path.isfile(newLoc):
        try:
            shutil.copy2(oldLoc, newLoc)
            print 'File ' + f + ' copied.'
        except IOError:
            print 'file "' + f + '" already exists'

I am sorry I am being handful but I thought I had solved it.

like image 388
worer Avatar asked Sep 14 '25 13:09

worer


2 Answers

In theory, there is no way to access your phone's internal memmory with a drive letter, because, Android connects as an MTP device, and not as a Mass Storage device. But, there are some weird solutions:

  1. Root the phone and get a application which enables "Mass Storage" .
  2. If you can not root and if(only if) both the computer are on the same network, run FTP server in you phone, and you get access for file copy by ftp.

But for you case I recommend adb- adb push C:\src /phone_destination is the best solution.You can google and easily find out way to do this in python.

like image 193
Rilwan Avatar answered Sep 16 '25 03:09

Rilwan


Google offers adb-sync, which is also available in python. This allows backup/synchronization of files on android device to PC.

The following github repo provides instructions on how to setup the process ie: enable USB Debugging, etc... however I suggest installing 15 second adb installer as opposed to downloading/installing the massive Android SDK just to get adb.

adb-sync: https://github.com/google/adb-sync
15 Sec ADB installer: https://forum.xda-developers.com/showthread.php?t=2588979


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!