Ryniek90 wrote:
Hi.
Last time i've got problem with sending big files, but i've already
dealt with it.
Now, when i want send directory (with some files in it) i iterate that
directory for files in it, and then in while loop open iterated files,
read them and send. But something's not working. It iterate's first file
from directory and sends it all the time (only first file). So, the
iteration is written bad. Could you tell me how to modify it?
Here's that method:
def send_dir(self):
print "Gathering information's..."
(FYI, "information" doesn't have a plural, and if it did it wouldn't
have an apostrophe!)
self.direc = os.walk(self.data)
for files in self.direc:
lst_files = files[2]
Please not that you're binding files[2] to lst_files each time so that
it's final value is whatever the final result of self.direc was. I don't
know whether that's intentional.
for fl in lst_files:
print "Sending %s from directory %s" % (fl,
os.path.split(self.data)[1])
while True:
files = open("%s/%s" % (self.data,fl), 'rb')
self.read = files.read(51200)
self.konn.send(self.read)
if not self.read:
break
> files.close()
This 'while' loop is opening a file and sending the first 51200 bytes,
but then either leaving the loop if nothing was read or repeating the
whole thing if something was read. I think you need:
data_file = open("%s/%s" % (self.data, fl), 'rb')
while True:
data = files.read(51200)
if not data:
break
self.konn.sendall(data)
data_file.close()
Notice that I used sendall() instead of send(). This is because send()
doesn't guarantee to send _all_ the data (it's in the documentation).
"self.data" is variable for the "self.konn.recv(4096") method.
self.konn is from "self.konn, self.addr = self.sok.accept()"
"self.direc = os.walk(self.data)
for files in self.direc:
lst_files = files[2]"
Iteration result looks like this:
>>> import os
>>> direc = os.walk('/media/DOWNLOAD/Obrazy płyt/Half-Life 2')
>>> for files in direc:
lst_files = files[2]
>>> lst_files
['hl2_1.nrg', 'hl2_2.nrg', 'hl2_4.nrg', 'hl2_5.nrg']
>>> for fl in lst_files:
print fl
hl2_1.nrg
hl2_2.nrg
hl2_4.nrg
hl2_5.nrg
>>>
--
http://mail.python.org/mailman/listinfo/python-list