It is currently Tue Apr 15, 2014 8:18 pm




Post new topic Reply to topic  [ 7 posts ] 
 Copy file from FTP server to New file in Machine. 
Author Message
Trooper
Trooper

Joined: Tue Jul 17, 2012 2:20 pm
Posts: 14
Post 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.


Fri Jul 20, 2012 12:30 pm
Profile
Moderator
Moderator

Joined: Sun Apr 02, 2006 2:46 pm
Posts: 1982
Location: Sacramento, CA
Post Re: Copy file from FTP server to New file in Machine.
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.

_________________
Travis Cox
Inductive Automation
Technical Support Rep.


Mon Jul 23, 2012 7:44 am
Profile WWW
Trooper
Trooper

Joined: Tue Jul 17, 2012 2:20 pm
Posts: 14
Post Re: Copy file from FTP server to New file in Machine.
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


Mon Jul 23, 2012 8:47 am
Profile
Moderator
Moderator

Joined: Sun Apr 02, 2006 2:46 pm
Posts: 1982
Location: Sacramento, CA
Post Re: Copy file from FTP server to New file in Machine.
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))
Put the close before the last write.

_________________
Travis Cox
Inductive Automation
Technical Support Rep.


Mon Jul 23, 2012 10:09 am
Profile WWW
Trooper
Trooper

Joined: Tue Jul 17, 2012 2:20 pm
Posts: 14
Post Re: Copy file from FTP server to New file in Machine.
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 .


Mon Jul 23, 2012 11:17 am
Profile
Trooper
Trooper

Joined: Tue Jul 17, 2012 2:20 pm
Posts: 14
Post Re: Copy file from FTP server to New file in Machine.
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?


Fri Sep 14, 2012 2:49 pm
Profile
Captain
Captain

Joined: Fri Oct 07, 2011 5:41 am
Posts: 81
Post Re: Copy file from FTP server to New file in Machine.
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()


Wed Nov 21, 2012 2:48 pm
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: