简体   繁体   中英

Any way to increase speed of closing a file?

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM