ftp.storlines error

M

Mik0b0

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')
text='test'
file.write(text)
ftp.storlines('STOR ' + file_to_change,file)
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?
 
C

Chris Rebert

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
 
M

Mik0b0

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?

Thanks Chris,
the final version looks like this and it works:

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.php'
i=1
i=open(file_to_change,'w')
text='test'
i.write(text)
i.close()
i=open(file_to_change,'rb')
ftp.storbinary('STOR ' + file_to_change,i)
ftp.retrlines('LIST')
i.close()
 
G

Gabriel Genellina

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')
text='test'
file.write(text)
ftp.storlines('STOR ' + file_to_change,file)
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

For the ftp client to be able to read and upload the file, it must have
been opened for reading. But you opened it with mode 'w' a few lines above.
A quick and dirty way would be to close the file right after
file.write(...) and re-open it with mode 'r':

...
file.write(text)
file.close()
file = open(file_to_change,'r')
ftp.storlines(...)

But I'd separate file-creation from file-uploading. When it's time to
upload the file, assume it already exists and has the desired contents
(because it has already been created earlier by the same script, or
perhaps by some other process), so you just have to open it with mode 'r'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top