Copy file from FTP server to New file in Machine

File on machine is getting created but not showing any data.
Have Test.txt file on server and want to copy the content of it to new file on machine.

Following is the code.

def getTestFile():
import os
import system
from ftplib import FTP

ftp = FTP(‘server name’)
ftp.login('ID,‘password’)

filename = system.file.getTempFile(“txt”)
s=ftp.retrbinary(‘RETR Test.txt’, open(filename,‘wb’).write)
print s

system.file.writeFile(“C:\TempTest\NewTest.txt”, system.file.readFileAsBytes(filename))
ftp.close()

Thank you in advance.

Try running that code in the script playground in the designer (under Tools in the file menu) to see if it returns any errors. If so let us know.

This is my code. : -

import os
import system
from ftplib import FTP

ftp = FTP(‘server’)
ftp.login(‘ID’,‘password’)
print “File List:”
ftp.dir()
s=ftp.retrbinary(‘RETR Test.txt’, open(‘C:\Temp\Test.txt’,‘wb’).write)
print s
ftp.close()

I tried running in Interactive Script, Its NOT giving any error. and output is :-

File List:
drwxr-xr-x 2 ftp ftp 512 Jul 12 16:50 .
drwxr-xr-x 2 ftp ftp 512 Jul 12 16:50 …
-rw-r–r-- 1 ftp ftp 266906192 Jun 8 13:04 1107d WCLF.zip
-rw-r–r-- 1 ftp ftp 107 Jul 12 16:49 Test.txt
226 Transfer complete

Note:- Its changing the modification time and date of Temp.txt on my computer tto time I run the code. But no data is shown in it. (0 kb file)

Looking forward for your help.!

Thanks

You should probably do the following:[code]def getTestFile():
import os
import system
from ftplib import FTP

ftp = FTP('server name')
ftp.login('ID,'password') 

filename = system.file.getTempFile("txt") 
s=ftp.retrbinary('RETR Test.txt', open(filename,'wb').write)
print s
ftp.close()

system.file.writeFile("C:\\TempTest\\NewTest.txt", system.file.readFileAsBytes(filename))

[/code]Put the close before the last write.

Thanks you Tavix for your promp reply.

For txt file this code not working. I dont know why ? if you can help out. ! I tried for docx file. Its copying the content and storing.

But when I try to open the docx file its giving a error in msword

“The File NewTest cannot be opened because there are problems with the content”

Then I say OK to that and new pop up

“Word found unreadable content in NewTest. Do you want to recover the contents of this document ? If you trust the source of this document.”

Then I say yes and its displaying the content.

Same occcurs when I try rar file or zip file.

Also if use txt . Its empty.

Hope you got me.

Thanks in advance .

Hi,

I am really trying hard to figure out that when I run ftp binary file retrieval. It downloads some .csv file size 160kb + perfectly. but when it comes across 2kb .csv file , its downloading it as and empty file.?

Can some one help on this?

Hello,
it can be helped you

[code]def readerFtp():
import ftplib
import system

ftp = ftplib.FTP('yourserverftp.com')
ftp.login('user','login')
files = []
ftp.retrlines('NLST', files.append)
for filename in files:

			def callback(buf):
				ftmp=system.file.getTempFile("dat")
				print ftmp
				f=open(ftmp, 'wb')
				f.write(buf)
				f.close()

			ftp.retrbinary('RETR '+filename, callback)

ftp.voidcmd('QUIT')
ftp.close()[/code]