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

标签 android python

我正在寻找一种解决方案,用于在连接此设备后将所有文件从硬盘驱动器上的特定目录复制到我的 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 中执行此操作的方法。

关于android - Python自动化脚本文件传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34118299/

相关文章:

android - 从 RecyclerView 中删除一个项目(ViewHolder 有 onClick 但适配器有数据集)

python - 在opencv中使用倒带

Python 返回 "wrong"退出代码

python - 如何在 Python 中的两个字符串项之间放置一个空格

python - 我如何重载\覆盖 Python 的断言语句以在断言通过时打印

python - 如何访问查询结果?

java - 如何在 Mapbox 中将折线叠加添加到静态图像?

java - sqlite不更新android中的记录

java - 为什么我的模型上的静态方法在 Google Cloud Endpoints 客户端库中不可用?

java - Eclipse,单击按钮时的新 Activity