Reading and Writing Device Files¶
The file read/write interface allows you to easily use this function to upload or download files from the device. It supports downloading and uploading large files. You can use the relevant interfaces to download files to local storage or memory, upload files from memory or local storage to the device, and also includes functions such as deleting files, modifying file permissions, and getting file information.
For files that do not exist or have no permissions, calling will cause native Python exceptions such as OSError
, FileNotFoundError
.
Downloading File to Local¶
This interface can download files from the device to local storage. In the example below, the content of the file /verity_key
on the device will be downloaded to the current computer’s my_file.txt
file. Note that the downloaded file will not retain the original permission information.
d.download_file("/verity_key", "my_file.txt")
>>> d.download_file("/adb_keys", "my_file.txt")
name: "adb_keys"
path: "/adb_keys"
st_mode: 33188
st_atime: 1230768000
st_mtime: 1230768000
st_ctime: 1230768000
st_size: 2202
>>> result = d.download_file("/adb_keys", "my_file.txt")
>>> print (result.st_mtime)
1230768000
>>> os.chmod("my_file.txt", result.st_mode)
Downloading File to Memory¶
This interface can download files from the device into memory (via BytesIO).
from io import BytesIO
fd = BytesIO()
d.download_fd("/verity_key", fd)
print (fd.getvalue())
Downloading File to Descriptor¶
This interface can download files from the device to a file descriptor. Note that the file needs to be opened in wb
binary mode.
fd = open("my_file.txt", "wb")
d.download_fd("/verity_key", fd)
Uploading File to Device¶
This interface can upload local files to the device. In the example, it means uploading the local 测试文件.txt
to the device’s /data/usr/file.txt
file. Uploading files also does not retain the original permission information.
d.upload_file("测试文件.txt", "/data/usr/file.txt")
>>> d.upload_file("file.txt", "/data/usr/file.txt")
name: "file.txt"
path: "/data/usr/file.txt"
st_mode: 33184
st_atime: 1230768000
st_mtime: 1230768000
st_ctime: 1230768000
>>> result = d.upload_file("file.txt", "/data/usr/file.txt")
>>> print (result.st_size)
0
Uploading File from Memory¶
In the following example, the file content in memory is uploaded to the device’s /data/usr/file.txt
file via BytesIO.
from io import BytesIO
d.upload_fd(BytesIO(b"fileContent"), "/data/usr/file.txt")
Uploading File from Descriptor¶
In the following example, the content of the local file myfile.txt
is uploaded to the device’s /data/usr/file.txt
file via fd descriptor. Note that the local file needs to be opened in rb
binary mode.
fd = open("myfile.txt", "rb")
d.upload_fd(fd, "/data/usr/file.txt")
Deleting Device File¶
This interface is used to delete files on the device.
d.delete_file("/data/usr/file.txt")
>>> d.delete_file("/data/usr/file.txt")
True
>>> d.delete_file("/adb_keys")
Traceback (most recent call last):
File "<console>", line 1, in <module>
OSError: [Errno 30] Read-only file system
Modifying File Permissions¶
This interface is used to modify file permissions on the device.
d.file_chmod("/data/usr/file.txt", mode=0o777)
>>> d.file_chmod("/data/usr/file.txt", mode=0o777)
name: "file.txt"
path: "/data/usr/file.txt"
st_mode: 33279
st_atime: 1230768000
st_mtime: 1230768000
st_ctime: 1230768000
>>> result = d.file_chmod("/data/usr/file.txt", mode=0o777)
>>> print (oct(result.st_mode))
0o100777
Getting File Information¶
This interface is used to get information about files on the device.
d.file_stat("/data/usr/file.txt")
>>> d.file_stat("/data/usr/file.txt")
name: "file.txt"
path: "/data/usr/file.txt"
st_mode: 33279
st_atime: 1230768000
st_mtime: 1230768000
st_ctime: 1230768000
>>> result = d.file_stat("/data/usr/file.txt")
>>> print (result.name)
'file.txt'