【发布时间】:2020-04-03 15:11:16
【问题描述】:
我正在尝试将整个视频文件上传到 RAM 以便快速访问。我希望文件保持原样,无需任何解码等。我只想指向 RAM 中的一个位置,而不是远程驱动程序。该文件只有 2 GB。我有 128 GB 内存。我需要逐帧分析,并且从服务器准备好需要很长时间。
我想我会做这样的事情
with open('my_file.txt', 'r') as f:
file_content = f.read() # Read whole file in the file_content string
print(file_content)
但我可能会出错。还有另一种方法吗?喜欢使用 IO 库吗?
In [11]: u = open("/net/server/raw/2020.04.02/IMG_9261.MOV",'r')
In [12]: data = u.read()
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-12-eecbc439fbf0> in <module>
----> 1 data = u.read()
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/codecs.py in decode(self, input, final)
320 # decode input (taking the buffer into account)
321 data = self.buffer + input
--> 322 (result, consumed) = self._buffer_decode(data, self.errors, final)
323 # keep undecoded input until the next call
324 self.buffer = data[consumed:]
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in position 31: invalid continuation byte
此示例使用 requests.get,但这仅适用于 HTTP 我有一个可以使用 NFS 挂载的本地服务器
import requests
from pygame import mixer
import io
r = requests.get("http://example.com/somesmallmp3file.mp3")
inmemoryfile = io.BytesIO(r.content)
mixer.music.init()
mixer.music.load(inmemoryfile)
mixer.music.play()
【问题讨论】:
-
尝试以
rb模式而不是r模式打开文件。前者用于从文件中读取字节,后者用于人类可读的文本。