题
当它打开时,我会收到错误“内存错误”并读取大于500MB的文件。 如果它不到500MB,它就完美无缺.. 即时使用我的进度栏的maxvalue大小
self.ftp = FTP(hostname)
self.ftp.login(user, password)
self.f = open(self.filename,'rb')
with open(self.filename,'rb') as filein:
self.size = filein.read()
self.size = len(self.size)
. 解决方案
Don't use the read()
method since it reads the whole file into a string. You should use the os.stat()
function to get the the file metadata, which returns a stat structure with the member st_size
. That's the size in bytes of the file. You don't have to read it all in first.
For sending, also read in and write out in chunks (say 16kB), in a loop.
其他提示
So, filein.read() actually reads (i.e. downloads) the file and is consuming your memory.
You can instead use
self.size = self.ftp.size(self.filename)
不隶属于 StackOverflow