ftp.storlines error

Chris Rebert clp2 at rebertia.com
Sun Jan 31 23:19:49 CET 2010


On Sun, Jan 31, 2010 at 2:07 PM, Mik0b0 <newssw at gmail.com> wrote:
> Good day/night/etc.
> I am rather a newb in Python (learning Python 3). I am trying to
> create a small script for FTP file uploads  on my home network. The
> script looks like this:
>
> from ftplib import FTP
> ftp=FTP('10.0.0.1')
> ftp.login('mike','*****')
> directory='/var/www/blabla/'
> ftp.cwd(directory)
> ftp.retrlines('LIST')
> print('<- - - - - - - - - >')
> file_to_change='test'
> file=1
> file=open(file_to_change,'w')

Here you open the file in (w)rite mode. Also, don't call it `file` as
that shadows the name of the built-in type.

> text='test'
> file.write(text)

And indeed, here you've written something to it.

> ftp.storlines('STOR ' + file_to_change,file)

storlines() needs a file opened in (r)ead mode however, hence the
error. Obviously, it needs to read the contents of the file in order
to send it over the network.
Either do the writing separately, close the file, and then open it
again in read mode, or open it in one of the modes that allows for
both reading and writing [see help(open) for details].

Cheers,
Chris
--
http://blog.rebertia.com

> ftp.retrlines('LIST')
> file.close()
>
> The output is like this:
> Traceback (most recent call last):
>  File "ftp.py", line 13, in <module>
>    ftp.storlines('STOR ' + file_to_change,i)
>  File "/usr/lib/python3.1/ftplib.py", line 474, in storlines
>    buf = fp.readline()
> IOError: not readable
>
> What is wrong?



More information about the Python-list mailing list