首页 > 解决方案 > 从文本读取 USB 设备中的文件。(C++/Python3)

问题描述

`

texttobecopied = open('mtp://%5Busb%3A001,015%5D/Internal%20stora/AppInventor/data/Scan_result.txt', 'r').readlines()

//opening the text file which I want to read. This is located in the USB tethered smartphone. The mtp://.... part is the path for that file.

appendFile  = open('destinationFile.txt', 'a')

//opened the destination file into which I want to write.
appendfile.write('\n')
appendFile.write(texttobecopied)

//tried to write that text from source file into my destination file .

appendFile.close()

初学者在这里。

我需要一个程序,它从.txt位于我的 USB 系留电话(内部存储)中的文件中读取文本并将该文本写入我系统上的文件中。

我通过指定路径(传统object.open('path/name'.'r')方式)尝试了传统方式,但它不起作用。

有没有办法我可以做到这一点?我不想复制包含文本的文件,我只需要里面的文本。

标签: python-3.x

解决方案


它是 Gnome 虚拟文件系统守护进程 ( gvfsd) 在您背后工作...

首先,手机的文件系统必须安装在某个地方,它不在通常的地方(/mnt/, /media/),但它会注册在/etc/mtab,不是吗?

在我的情况下,它在最后,它安装在一个名为 my 的运行时目录中UID

$ grep "/$UID/" /etc/mtab
gvfsd-fuse /run/user/1000/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0

至此我知道了手机文件系统在电脑文件系统中的挂载点,我可以列出它的内容

$ ls /run/user/1000/gvfs
'mtp:host=%5Busb%3A001%2C006%5D'/

我知道手机的内容,因此我可以使用 Python 打开我设备上的文件之一

$ python -c "open('/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C006%5D/Internal shared storage/Ringtones/08_River.mp3', 'rb')"
$

如您所见,打开文件没有错误。

我希望你能根据你的具体情况调整这个食谱并接受我的回答。


推荐阅读