简体   繁体   English

在 Python 中使用文件和数据

[英]Using files and data in Python

Can we store images, audio and video or other files like PDF in any dictionary or files in Python?我们可以在任何字典或 Python 文件中存储图像、音频和视频或其他文件(如 PDF)吗? How can it be done?怎么做?

Yes.是的。 Read it as raw binary data and Python will not care.将其作为原始二进制数据读取,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 }

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

And see: https://docs.python.org/2/library/stdtypes.html#bltin-file-objects并参见: https : //docs.python.org/2/library/stdtypes.html#bltin-file-objects

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM