我想直接从FTP服务器读取WAV文件(位于FTP服务器中),而不用Python将其下载到PC中。有可能吗?如果可以,怎么办?
我尝试了此解决方案Read a file in buffer from ftp python,但没有成功。我有.wav音频文件。我想读取该文件并从该.wav文件中获取详细信息,例如文件大小,字节速率等。
我的代码可以在其中本地读取WAV文件:
import struct
from ftplib import FTP
global ftp
ftp = FTP('****', user='user-****', passwd='********')
fin = open("C3.WAV", "rb")
chunkID = fin.read(4)
print("ChunkID=", chunkID)
chunkSizeString = fin.read(4) # Total Size of File in Bytes - 8 Bytes
chunkSize = struct.unpack('I', chunkSizeString) # 'I' Format is to to treat the 4 bytes as unsigned 32-bit inter
totalSize = chunkSize[0]+8 # The subscript is used because struct unpack returns everything as tuple
print("TotalSize=", totalSize)
答案 0 :(得分:0)
要快速实施,您可以使用以下网址的FtpFile
类:
Get files names inside a zip file on FTP server without downloading whole archive
ftp = FTP(...)
fin = FtpFile(ftp, "C3.WAV")
# The rest of the code is the same
该代码效率不高,因为每个fin.read
都会打开一个新的下载数据连接。
要获得更有效的实现,只需一次下载整个标头(我不知道WAV标头结构,我在这里下载10 KB作为示例):
from io import BytesIO
ftp = FTP(...)
fin = BytesIO(FtpFile(ftp, "C3.WAV").read(10240))
# The rest of the code is the same