tags:

views:

670

answers:

3

I had an PHP script to update the webserver from a local directory. I'm migrating the script into Python, it works almost OK but after a PUT command the size of the file in the server isn't the same as the local file. Once I download again the file from the FTP server, the only difference is the CR/LF mark. It annoys me because the same script is comparing the size of the files to update. Also, it works perfectly in PHP vía ftp_put.

from ftplib import FTP

ftpserver = "myserver"
ftpuser = "myuser"
ftppass = "mypwd"

locfile =  "g:/test/style.css"
ftpfile =  "/temp/style.css"

try:
    ftp = FTP(ftpserver, ftpuser, ftppass)
except:
    exit ("Cannot connect")

f = open (locfile, "r")
try:
    ftp.delete (ftpfile)
except:
    pass

# ftp.sendcmd ("TYPE I")
# ftp.storlines("STOR %s" % ftpfile, f)
ftp.storbinary("STOR %s" % ftpfile, f)
f.close()

ftp.dir (ftpfile)
ftp.quit()

###

Any suggestions?

TIA, Pablo

+7  A: 

Do you need to open the locfile in binary using "rb"?

f = open (locfile, "rb")

David Sykes
Thank you so much, this was the shot between the eyes I needed, I spent all weekend banging my head against the wall over this. This also applies to the reverse scenario, transferring from ftp.
lewisblackfan
@David:It works perfectly with your advice. Thanks! (a positive response in 2 minutes, wow)
PabloG
@PabloG Did you really make that comment just now, or have I been sucked into a time warp?
David Sykes
+1  A: 

Well if you go under the properties of your file in Windows or a *nix environment, you will notice two sizes. One is the sector size, and one is the actual size. The sector size is the number of sectors in bytes that are used up on your hard disk. That is because two files cannot be in the same sector with most modern file systems, so if your file fills up half of the sector the whole sector is marked as filled.

So you might be comparing the sector file size to the actual file size on the FTP server or vice versa.

Nick Berardi
A: 

Small files take up a whole node on the filesystem whatever size that is.

My host tends to report all small files as 4kb in ftp but in a shell gives an accurate size so it might be a 'feature' common to ftp clients.

sparkes