2

here is my code:

import ftputil
import urllib2
a_host = ftputil.FTPHost(hostname, username,passw)

for (dirname, subdirs, files) in a_host.walk("/"): # directory
    for f in files:
            if f.endswith('txt'):
                htmlfile = open(f, 'r')
                readfile = htmlfile.read()

I think it should be ok, but I got an error

Traceback (most recent call last):

    htmlfile = open(f, 'r')
IOError: [Errno 2] No such file or directory: u'readme.txt'

where is the problem?

gyula
  • 229
  • 3
  • 7
  • 12
  • possible duplicate of [Python: download a file over an FTP server](http://stackoverflow.com/questions/11768214/python-download-a-file-over-an-ftp-server) – Guy L Aug 01 '15 at 08:24

2 Answers2

5

You cannot read the remote file using open like local file. You need to download the file from the remote host first.

for (dirname, subdirs, files) in a_host.walk("/"): # directory
    for f in files:
        if f.endswith('txt'):
            a_host.download(f, f)  # Download first
            with open(f) as txtfile:
                content = txtfile.read()
falsetru
  • 357,413
  • 63
  • 732
  • 636
1

You need to use a_host.open, not a Python default open.

Thus, instead:

htmlfile = open(f, 'r')
readfile = htmlfile.read()

This:

htmlfile = a_host.open(f, 'r')
readfile = htmlfile.read()
m0nhawk
  • 22,980
  • 9
  • 45
  • 73