【问题标题】:Check and wait until a file exists to read it检查并等待文件存在才能读取它
【发布时间】:2014-02-13 06:23:35
【问题描述】:

我需要等到文件创建后再读入。我有以下代码,但确定它不起作用:

import os.path
if os.path.isfile(file_path):
    read file in
else:
    wait

有什么想法吗?

【问题讨论】:

  • 循环几秒钟 time.sleep ?
  • while not os.path.isfile(): sleep(.1)?
  • 不要用isfile去检查文件是否存在,用exists再做stat
  • 你可以重复检查一个循环,但如果你想要更智能的东西,你可以使用 inotify:pypi.python.org/pypi/inotify/0.2.9

标签: python


【解决方案1】:

一个简单的实现可能是:

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)

每次检查后等待一定的时间,然后在路径存在时读取文件。如果从未创建文件,则可以使用 KeyboardInterruption 异常停止脚本。您还应该检查路径是否为文件,以避免一些不需要的异常。

【讨论】:

  • @thefourtheye 欺骗但固定:p
  • @thefourtheye:这有关系吗?如果在该路径上没有创建任何内容,程序也会永远等待。
  • @user2357112 正确。我们如何解决这个问题?异步库?
  • @thefourtheye:作为一个非常简单的解决方案,您能否添加一个计数器,以便脚本在 N 次尝试后退出?
  • @aim:你可以,但我们应该保持答案简单(不超过 10 行,没有任何变量),并让人们根据需要自定义它。
【解决方案2】:

下载文件或创建 file_path 后,以下脚本将中断,否则将等待长达 10 秒,以便下载文件或创建 file_path,然后再中断。

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

【讨论】:

    【解决方案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 问题出在os.path.lexists(...)。应该是os.path.lexists(...)。我相信这是一个错字,除非您编辑了 os.py 否则(?)
    • @MaximeLorant 我想它添加了变量定义和读取文件很有用的是那些对编程非常陌生的人遇到这个答案并需要一个功能示例。虽然他的解决方案只适用于纯文本文件和 Python 2。
    • 有点功能* 第一次检查确实应该是一个循环。
    【解决方案4】:

    此代码可以按文件大小检查下载。

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

    【讨论】:

      【解决方案5】:
      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)
          
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      • 此问题有四个现有答案,其中包括一个获得最高票数且获得近 一百票 的已接受答案。你确定你的解决方案还没有给出吗?如果不是,您为什么认为您的方法改进了已被社区验证的现有提案?在 Stack Overflow 上,提供解释总是很有用,但在问题已得到解决且让 OP 和社区都满意的情况下,它尤其很重要。通过解释您的答案有何不同以及何时可能更受欢迎,帮助读者了解答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 2014-12-28
      • 2013-12-30
      相关资源
      最近更新 更多