Reading and Writing Device Files

The file read/write interface allows you to easily upload files to and download files from the device, with support for large files. You can use these interfaces to download files to your local machine or into memory, and upload files from memory or your local machine to the device. Additional functions include deleting files, modifying file permissions, and retrieving file information. For files that do not exist or for which you do not have permission, the call will raise a native Python exception such as OSError or FileNotFoundError.

Download File to Local Machine

This interface can download a file from the device to your local machine. In the following example, the content of the file /verity_key on the device will be downloaded to the my_file.txt file on the current computer. Note that the downloaded file will not retain its 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 a File Descriptor

This interface can download a file from the device to a file descriptor. Note that the file needs to be opened in wb (write binary) mode.

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. In the example, it uploads the local file 测试文件.txt (Test File.txt) to /data/usr/file.txt on the device. Uploaded files also do not retain their 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

Upload File from Memory

The following example uploads file content from memory to /data/usr/file.txt on the device using BytesIO.

from io import BytesIO
d.upload_fd(BytesIO(b"fileContent"), "/data/usr/file.txt")

Upload File from a File Descriptor

The following example uploads the content of the local file myfile.txt to /data/usr/file.txt on the device via an fd (file descriptor). Note that the local file needs to be opened in rb (read binary) mode.

fd = open("myfile.txt", "rb")
d.upload_fd(fd, "/data/usr/file.txt")

Delete a Device File

This interface is used to delete a file 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

Modify File Permissions

This interface is used to modify the permissions of a file 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

Get File Information

This interface is used to get information about a file 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'