从文本读取USB设备中的文件。(C ++ / Python3)

时间:2018-08-05 06:44:21

标签: python-3.x

`

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()

初学者在这里。

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

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

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

1 个答案:

答案 0 :(得分:0)

这是Gnome虚拟文件系统后台驻留程序(gvfsd),在后台运行...

首先,电话的文件系统必须安装在某个地方,不在通常的位置(/mnt//media/)中,但应该将其注册在/etc/mtab中,它吗?

在我的情况下,它位于末尾,并安装在以我的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')"
$

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

我希望您可以根据自己的具体情况调整此食谱,并接受我的回答。