70

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?

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
speedyrazor
  • 3,127
  • 7
  • 33
  • 51

5 Answers5

116

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.

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
24

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")
SIM
  • 21,997
  • 5
  • 37
  • 109
3
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
Sakam24
  • 121
  • 5
  • i did't find any error just i run just check file_path. – Sakam24 Feb 13 '14 at 07:26
  • @Sakam24 The problem is the `os.path.lexists(...)`. It should be `os.path.lexists(...)`. I beleive this is a typo, unless you edited `os.py` otherwise(?) – WorkingRobot Mar 25 '16 at 04:25
  • @MaximeLorant I suppose it adds the variable definition and the reading of the file that would be useful is someone who is really new to programming encounters this answer and needs a functional example. Although his solution would only work for plain text files and under Python 2. – EndermanAPM Dec 22 '16 at 11:08
  • Somewhat functional* The 1st check really should be a loop. – EndermanAPM Dec 22 '16 at 11:29
2
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)
    
SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 31 '21 at 04:54
  • 1
    There are **four existing answers** to this question, including a top-voted, accepted answer with nearly **one hundred votes**. Are you _certain_ your solution hasn't already been given? If not, why do you believe your approach improves upon the existing proposals, which have been validated by the community? Offering an explanation is _always_ useful on Stack Overflow, but it's _especially_ important where the question has been resolved to the satisfaction of both the OP and the community. Help readers out by explaining what your answer does different and when it might be preferred. – Jeremy Caney Jan 01 '22 at 00:35
1

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")
Community
  • 1
  • 1
Nori
  • 2,340
  • 1
  • 18
  • 41