Check and wait until a file exists to read it

前端 未结 4 2257
太阳男子
太阳男子 2020-12-04 16:50

I need to wait until a file is created then read it in. I have the below code, but sure it does not work:

import os.path
if os.path.isfile(file_path):
    re         


        
4条回答
  •  时光取名叫无心
    2020-12-04 17:18

    This code can check download by file size.

    import os, sys
    import time
    
    def getSize(filename):
        if os.path.isfile(filename): 
            st = os.stat(filename)
            return st.st_size
        else:
            return -1
    
    def wait_download(file_path):
        current_size = getSize(file_path)
        print("File size")
        time.sleep(1)
        while current_size !=getSize(file_path) or getSize(file_path)==0:
            current_size =getSize(file_path)
            print("current_size:"+str(current_size))
            time.sleep(1)# wait download
        print("Downloaded")
    

提交回复
热议问题