File Read/Write¶
The file read/write interfaces allow you to easily upload or download files from a device, supporting large file transfers. You can use the relevant interfaces to download files to local storage or memory, or upload files from memory or local storage to the device. Additionally, it supports functions such as deleting files, modifying file permissions, and obtaining file information.
If the file does not exist or lacks permissions, the call will raise native Python exceptions, such as OSError or FileNotFoundError.
Download File to Local¶
This interface can download a file from the device to local storage. In the example below, the content of the file /verity_key on the device is downloaded to a local file named my_file.txt. 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)
Download File to Memory¶
This interface can download a file from the device into memory (via BytesIO).
from io import BytesIO
fd = BytesIO()
d.download_fd("/verity_key", fd)
print(fd.getvalue())
Download File to Descriptor¶
This interface can download a file from the device to a file descriptor. Note that the file must be opened in binary write mode (wb).
fd = open("my_file.txt", "wb")
d.download_fd("/verity_key", fd)
Upload File to Device¶
This interface can upload a local file to the device. The example below uploads the local file 测试文件.txt to /data/local/tmp/file.txt on the device. Uploading a file also does not preserve the original permission information.
d.upload_file("测试文件.txt", "/data/local/tmp/file.txt")
>>> d.upload_file("file.txt", "/data/local/tmp/file.txt")
name: "file.txt"
path: "/data/local/tmp/file.txt"
st_mode: 33184
st_atime: 1230768000
st_mtime: 1230768000
st_ctime: 1230768000
>>> result = d.upload_file("file.txt", "/data/local/tmp/file.txt")
>>> print(result.st_size)
0
Upload File from Memory¶
The example below uploads the content of a file in memory via BytesIO to /data/local/tmp/file.txt on the device.
from io import BytesIO
d.upload_fd(BytesIO(b"fileContent"), "/data/local/tmp/file.txt")
Upload File from Descriptor¶
The example below uploads the content of the local file myfile.txt via a file descriptor to /data/local/tmp/file.txt on the device. Note that the local file must be opened in binary read mode (rb).
fd = open("myfile.txt", "rb")
d.upload_fd(fd, "/data/local/tmp/file.txt")
Delete Device File¶
This interface is used to delete a file on the device.
d.delete_file("/data/local/tmp/file.txt")
>>> d.delete_file("/data/local/tmp/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
Modify File Permissions¶
This interface is used to modify file permissions on the device.
d.file_chmod("/data/local/tmp/file.txt", mode=0o777)
>>> d.file_chmod("/data/local/tmp/file.txt", mode=0o777)
name: "file.txt"
path: "/data/local/tmp/file.txt"
st_mode: 33279
st_atime: 1230768000
st_mtime: 1230768000
st_ctime: 1230768000
>>> result = d.file_chmod("/data/local/tmp/file.txt", mode=0o777)
>>> print(oct(result.st_mode))
0o100777
Get File Information¶
This interface is used to obtain information about a file on the device.
d.file_stat("/data/local/tmp/file.txt")
>>> d.file_stat("/data/local/tmp/file.txt")
name: "file.txt"
path: "/data/local/tmp/file.txt"
st_mode: 33279
st_atime: 1230768000
st_mtime: 1230768000
st_ctime: 1230768000
>>> result = d.file_stat("/data/local/tmp/file.txt")
>>> print(result.name)
'file.txt'