pretyv5
pretyv5

Reputation: 105

download file from ftp server using Python

I am trying to download csv files from ftp server but I receive these error. I don't know where I am going wrong.

#!/usr/bin/python

import ftplib


ftp = ftplib.FTP('192.168.0.00', 'bingo', 'word')
files = ftp.dir('/')
ftp.cwd("/")
filematch = '*.csv'
target_dir = '/home/toor/ringolist'
import os

for filename in ftp.nlst(filematch):
    target_file_name = os.path.join(target_dir,os.path.basename(filename))
    with open(target_file_name,'wb') as fhandle:
            ftp.retrbinary('RETR %s' %filename, fhandle.write))

errors:


sudo ./ftp_ringo.py
: not found.py: 1: ./ftp_ringo.py:
: not found.py: 3: ./ftp_ringo.py:
./ftp_ringo.py: 4: ./ftp_ringo.py: import: not found
: not found.py: 5: ./ftp_ringo.py:
./ftp_ringo.py: 6: ./ftp_ringo.py: Syntax error: "(" unexpected

Upvotes: 1

Views: 4075

Answers (2)

pretyv5
pretyv5

Reputation: 105

This is my updated it works fine in downloading the file :

#!/usr/bin/python

import ftplib

ftp = ftplib.FTP('192.168.0.00', 'bingo', 'word')
files = ftp.dir('/')
ftp.cwd("/")
filematch = '*.csv'
target_dir = '/home/toor/ringolist'
import os

for filename in ftp.nlst(filematch):
    target_file_name = os.path.join(target_dir,os.path.basename(filename))
    with open(target_file_name,'wb') as fhandle:
            ftp.retrbinary('RETR %s' %filename, fhandle.write)


        # deletes the files from the FTP after transfer
            ftp.delete(os.path.basename(filename))

Upvotes: 1

ForceBru
ForceBru

Reputation: 44838

It's not the problem with the code, the problem's in the way you're trying to call it. Try sudo python code.py or do which python and use the result string instead of /usr/bin/python in your script.

Upvotes: 1

Related Questions