1

I used the ftputil module for this, but ran into a problem that it doesn't support 'a'(append) appending to the file, and if you write via 'w' it overwrites the contents.

That's what I tried and I'm stuck there:

with ftputil.FTPHost(host, ftp_user, ftp_pass) as ftp_host:
      with ftp_host.open("my_path_to_file_on_the_server", "a") as fobj:
         cupone_wr = input('Enter coupons with a space: ')
         cupone_wr = cupone_wr.split(' ')
         for x in range(0, len(cupone_wr)):
             cupone_str = '<p>Your coupon %s</p>\n' % cupone_wr[x]
             data = fobj.write(cupone_str)
         print(data)

The goal is to leave the old entries in the file and add fresh entries to the end of the file every time the script is called again.

bad_coder
  • 11,289
  • 20
  • 44
  • 72

2 Answers2

1

Indeed, ftputil does not support appending. So either you will have to download complete file and reupload it with appended records. Or you will have to use another FTP library.

For example the built-in Python ftplib supports appending. On the other hand, it does not (at least not easily) support streaming. Instead, it's easier to construct the new records in-memory and upload/append them at once:

from ftplib import FTP
from io import BytesIO

flo = BytesIO() 

cupone_wr = input('Enter coupons with a space: ')
cupone_wr = cupone_wr.split(' ')
for x in range(0, len(cupone_wr)):
    cupone_str = '<p>Your coupon %s</p>\n' % cupone_wr[x]
    flo.write(cupone_str)

ftp = FTP('ftp.example.com', 'username', 'password')

flo.seek(0)
ftp.storbinary('APPE my_path_to_file_on_the_server', flo)
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
1

ftputil author here :-)

Martin is correct in that there's no explicit append mode. That said, you can open file-like objects with a rest argument. In your case, rest would need to be the original length of the file you want to append to.

The documentation warns against using a rest argument that points after the file because I'm quite sure rest isn't expected to be used that way. However, if you use your program only against a specific server and can verify its behavior, it might be worthwhile to experiment with rest. I'd be interested whether it works for you.

sschwarzer
  • 163
  • 7
  • Thanks to everyone and to you, too, for the answers. The problem was solved this way: Opened the file, saved the old records in 'r' mode into a variable. Then I wrote the new ones in 'w' mode and merged both records at the end. – Oleg Paslavskiy Sep 14 '21 at 22:43
  • @OlegPaslavskiy Thanks for the feedback. Can you please describe in more detail which of these steps happened on the server vs. the client side and how you merged the files? Do you mean you created the complete file on the client side and uploaded it to the server? – sschwarzer Sep 16 '21 at 09:11