Check and wait until a file exists to read it

Python

Python Problem Overview


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):
    read file in
else:
    wait

Any ideas please?

Python Solutions


Solution 1 - Python

A simple implementation could be:

import os.path
import time

while not os.path.exists(file_path):
    time.sleep(1)

if os.path.isfile(file_path):
    # read file
else:
    raise ValueError("%s isn't a file!" % file_path)

You wait a certain amount of time after each check, and then read the file when the path exists. The script can be stopped with the KeyboardInterruption exception if the file is never created. You should also check if the path is a file after, to avoid some unwanted exceptions.

Solution 2 - Python

The following script will break as soon as the file is dowloaded or the file_path is created otherwise it will wait upto 10 seconds for the file to be downloaded or the file_path to be created before breaking.

import os
import time

time_to_wait = 10
time_counter = 0
while not os.path.exists(file_path):
    time.sleep(1)
    time_counter += 1
    if time_counter > time_to_wait:break

print("done")

Solution 3 - Python

import os
import time
file_path="AIMP2.lnk"
if  os.path.lexists(file_path):
    time.sleep(1)
    if os.path.isfile(file_path):
        fob=open(file_path,'r');
        read=fob.readlines();
        for i in read:
            print i
    else:
        print "Selected path is not file"
else:
    print "File not Found "+file_path

Solution 4 - Python

import os.path
import time

file_present = False

while file_present == False:
    if os.path.isfile(file_path):
       # read file in
       file_present = True
       break

    time.sleep(5)
    

Solution 5 - Python

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")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionspeedyrazorView Question on Stackoverflow
Solution 1 - PythonMaxime LorantView Answer on Stackoverflow
Solution 2 - PythonSIMView Answer on Stackoverflow
Solution 3 - PythonSakam24View Answer on Stackoverflow
Solution 4 - PythonSindeesh DineshView Answer on Stackoverflow
Solution 5 - PythonNoriView Answer on Stackoverflow