0

I'm trying to write a html-file and then upload it to my website using the following code:

webpage = open('testfile.html',"w")
webpage.write(contents)
webpage.close

server = 'ftp.xxx.be' 
username = 'userxxx'
password = 'topsecret'
ftp_connection = ftplib.FTP(server, username, password)
remote_path = "/"
ftp_connection.cwd(remote_path)
fh = open("testfile.html", 'rb')
ftp_connection.storbinary('STOR testfile.html', fh)
fh.close()                    

The problem is the .close command seems to be slower than the ftp connection and the file that is sent over ftp is empty. A few seconds after the ftp is executed I see the file correctly locally on my PC.

Any hints to be certain the .close is finished before the ftp starts (apart from using time.sleep())?

Running Python 3.xx on W7pro

martineau
  • 119,623
  • 25
  • 170
  • 301
ON5MF Jurgen
  • 113
  • 2
  • 7

1 Answers1

-1

Try blocking on the close call:

Blocking until a file is closed in python

By the way, are the parentheses missing on your close call?

user2842685
  • 104
  • 3
  • 8