我为此使用了 ftputil 模块,但遇到了一个问题,它不支持将 'a'(append) 附加到文件,如果你通过 'w' 写入,它会覆盖内容。
这就是我尝试过的,我被困在那里:
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)
目标是将旧条目保留在文件中,并在每次再次调用脚本时将新条目添加到文件末尾。