BASHEER AHMEED
BASHEER AHMEED

Reputation: 11

How to Copy Files to Android MTP by python?

I have a problem, I can't found path MTP device

When I try this script it gives me an error

raceback (most recent call last):
  File "C:\Users\BSHR\Desktop\Fire HD10 2019 Bootless\file.py", line 11, in <module>
    os.replace(source,destination)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\BSHR\\Desktop\\Fire HD10 2019 Bootless\\1.img' -> 'MTP:\\Fire\\Internal storage\\1xx.img'


import os

source = "C:\\Users\\BSHR\\Desktop\\Fire HD10 2019 Bootless\\1.img"

destination = "MTP:\\Fire\\Internal storage\\xx.img"

try:
    if os.path.exists(destination):
        print("file already")
    else:
        os.replace(source,destination)
        print(source+" was moved")
except FileNotFoundError:
    print(source+" was not found")


Transfer the file 1.img to via MTP

Upvotes: 1

Views: 1410

Answers (1)

Jack Hwang
Jack Hwang

Reputation: 171

There are two ways.

  1. Install a user mode window file system driver

    • Install Dokany on your Windows PC
    • Use mtpmount command line tool to mount the Android device on a driver letter

    This way your Python can access files on Android like with other file systems.

  2. Use PyMTP alike package in the Python scripts. This will ensure your script can run on any Windows PC but you have to use its APIs and cannot easily open/close files or traverse directories using GUI (e.g., file dialogs).

    import pymtp
    
    devices = pymtp.get_devices()
    device = devices[0]  # Assume only one device is connected
    # Connect to the device
    with pymtp.MTPDevice(device.device_entry) as mtp:
        # List the directories on the device
        dirs = mtp.get_folder_list()
        enter code here

Upvotes: 2

Related Questions