Python基本Q - 文件和数据

时间:2017-09-03 16:31:01

标签: android python python-2.7 python-3.x

我们可以将图像和音频以及视频或其他文件(如pdf)存储在任何字典或python中的文件中吗?如果是,那么请告诉我该怎么做。

1 个答案:

答案 0 :(得分:1)

是。将其作为原始二进制数据读取,Python不关心。

with open("/path/to/file.foo", "rb") as file_descriptor1:
    file_content_bytes = file_descriptor1.read()
# The "with" block will close the file descriptor for you

my_dictionary = { "file.foo": file_content_bytes }

请参阅:https://docs.python.org/2/library/functions.html#open

请参阅:https://docs.python.org/2/library/stdtypes.html#bltin-file-objects