Skip to main content
 首页 » 编程设计

android之Python自动化脚本文件传输

2025年05月04日14telwanggs

我正在寻找一种解决方案,用于在连接此设备后将所有文件从硬盘驱动器上的特定目录复制到我的 Android 手机上的特定或非特定目录。

我希望这些文件在我将手机连接到计算机并运行 .py 文件后自动移动(或至少复制)到我的手机。

我有 windows 7 和 python 2.7

我正在从另一个答案尝试这个,但我无法理解,因为解释很少,因此我无法让它工作。

编辑:我已经弄清楚如何在文件夹之间传输文件,但我想传输到我的手机。那么我该如何解决我的系统找不到手机路径的错误,我相信这会解决我的问题。代码工作正常问题是路径。

这是我的代码:

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' 

很抱歉,我的问题很少,但我认为我已经解决了它。

请您参考如下方法:

理论上,无法使用驱动器盘符访问手机的内部存储器,因为 Android 作为 MTP 设备而不是大容量存储设备进行连接。但是,有一些奇怪的解决方案:

  1. root 手机并获取启用“大容量存储”的应用程序。
  2. 如果您不能 root 并且(仅当)两台计算机都在同一网络上,请在您的手机中运行 FTP 服务器,然后您可以通过 ftp 访问文件复制。

但对于您的情况,我建议 adb - adb push C:\src/phone_destination 是最好的解决方案。您可以在 google 上轻松找到在 python 中执行此操作的方法。