# Copy file from FTP server to New file in Machine

**URL:** <https://forum.inductiveautomation.com/t/copy-file-from-ftp-server-to-new-file-in-machine/4856>\
**Category:** Archived Topics\
**Created:** [July 20, 2012, 8:30pm UTC](https://forum.inductiveautomation.com/t/copy-file-from-ftp-server-to-new-file-in-machine/4856 "2012-07-20T20:30:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmehta](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jmehta](https://forum.inductiveautomation.com/u/jmehta)\
**Post date:** [July 20, 2012, 8:30pm UTC](https://forum.inductiveautomation.com/t/copy-file-from-ftp-server-to-new-file-in-machine/4856/1 "2012-07-20T20:30:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [July 23, 2012, 3:44pm UTC](https://forum.inductiveautomation.com/t/copy-file-from-ftp-server-to-new-file-in-machine/4856/2 "2012-07-23T15:44:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmehta](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jmehta](https://forum.inductiveautomation.com/u/jmehta)\
**Post date:** [July 23, 2012, 4:47pm UTC](https://forum.inductiveautomation.com/t/copy-file-from-ftp-server-to-new-file-in-machine/4856/3 "2012-07-23T16:47:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [July 23, 2012, 6:09pm UTC](https://forum.inductiveautomation.com/t/copy-file-from-ftp-server-to-new-file-in-machine/4856/4 "2012-07-23T18:09:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmehta](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jmehta](https://forum.inductiveautomation.com/u/jmehta)\
**Post date:** [July 23, 2012, 7:17pm UTC](https://forum.inductiveautomation.com/t/copy-file-from-ftp-server-to-new-file-in-machine/4856/5 "2012-07-23T19:17:23Z")

</div>

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 .

---

<div class="post-metadata">

**Author:** ![jmehta](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jmehta](https://forum.inductiveautomation.com/u/jmehta)\
**Post date:** [September 14, 2012, 10:49pm UTC](https://forum.inductiveautomation.com/t/copy-file-from-ftp-server-to-new-file-in-machine/4856/6 "2012-09-14T22:49:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![2Gi\_Technologie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/2gi_technologie/32/3307_2.png) [@2Gi\_Technologie](https://forum.inductiveautomation.com/u/2Gi_Technologie)\
**Post date:** [November 21, 2012, 10:48pm UTC](https://forum.inductiveautomation.com/t/copy-file-from-ftp-server-to-new-file-in-machine/4856/7 "2012-11-21T22:48:05Z")

</div>

## 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]
```
