23

How can I read from and write to my Galaxy Nexus phone, using MTP over a USB cable in python? I'm on a windows 7 computer.

foosion
  • 6,807
  • 23
  • 58
  • 97

4 Answers4

7

I'm also looking for the same thing. There seems to be pymtp for cross-platform and wmdlib for Windows.

  • 1
    Not much documentation and examples to find on these modules. – Norfeldt Feb 10 '14 at 15:53
  • I ended up using and improving [pymtp](https://pypi.python.org/pypi/PyMTP), it works well on GNU/Linux and Mac OS X. You can see an example here: https://github.com/guardianproject/keysync/blob/master/otrapps/util.py – Hans-Christoph Steiner Feb 14 '14 at 23:11
  • 1
    I'm on a windows 7 and looking for a simple solution. pymtp depends on libmtp, which again depends on LibUSB and libiconv that has to be used to compile it... – Norfeldt Feb 16 '14 at 18:37
  • The linked code contains the comment "# Right now the win32 'sync' method is to prompt the user to manually copy the file over, so we always return true." :-( Not much help on any version of windows... – virtualnobi Dec 15 '17 at 18:52
6

Calibre, a popular ebook management program, has python and C source code to transfer files over an MTP connection. http://code.google.com/p/calibre-ebook/downloads/list

foosion
  • 6,807
  • 23
  • 58
  • 97
5

One way to do this would be to install ADB (android debugging bridge, part of the SDK) and launch it as a child process from python. ADB can be used to, among other things, read from or write to, an android device.

foosion
  • 6,807
  • 23
  • 58
  • 97
  • 5
    Not really a solution since it requires the phone to have usb debugging enabled, which might be ok for most of us but not for normal consumers. – Daniel F Feb 14 '14 at 15:22
  • 1
    @Norfeldt Basically, you have to install drivers for your OS and download the ADB files. Do you have that working? Then use the subprocess.Popen to run ADB, for example: res, err = subprocess.Popen([adb, 'push', pc_source, device_dest], stdout=subprocess.PIPE).communicate() – foosion Feb 15 '14 at 02:40
  • Not a solution since it requires USB debugging, and most users don't have this. MTP should work with USB debugging – Basj Sep 27 '17 at 15:32
-2

Simply connecting a USB cable between the phone and computer should work.

It may be necessary to enable MTP transfers in the settings menu on your phone. The menu selection location is likely to be different on different versions of android and different phone models. Try a google search for "galaxy nexus enable mtp". Make sure to include your android and phone version in the search.

Make sure it is a good quality USB cable. Poor quality cables will not make a good connection and therefor not work reliably.

A file management dialog comes up immediately on my desktop after hooking up a usb cable between my phone and laptop showing both the phone internal storage and SD card. This allows me to transfer files both ways directly to the phone SD storage (Linux Mint <-> LG Android ver. 5.1)

Note that it is also possible to transfer files using Bluetooth.

After establishing a connection, you would need to find the device name. Then it would be possible to open the device using standard python file constructs, i.e. popen(), etc.

Morse
  • 5,778
  • 5
  • 31
  • 55
Michael
  • 23
  • 6
  • 7
    It's not about cable. It's about how can you write a file on the phone (via FTP) with Python code. It's **not** just as simple as `with open('mtp://SamsungA3/hello.txt', 'w') as f: f.write('hello')`... – Basj Oct 03 '17 at 13:29
  • I down voted this - I agree it's not about the cable. The cable matters, but the code is not as straight forward as the other code used to open local files. – Python_Learner_DK Jan 10 '19 at 16:33