Python Forum
How to check if a file has finished being written
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if a file has finished being written
#1
I have a programme that writes an mtx file to a specified directory on my laptop. It's a very large file (~400,000 KB) and so takes quite some time to finish being written. The next block in my code is dependent on the mtx file being fully written but proceeds to execute once the mtx file first appears in the directory. I had used:
time.sleep(120)
between the two blocks to ensure the file was fully written, but I want a more robust way of checking as this sleep time may not always work. Any ideas on how to check that the file has finished being written? I've had a look at the watchdog module but I can't wrap my head around the syntax. Thank you.
Reply
#2
Can check size of file and time.sleep() alone should not be used,
better to use schedule either from OS or eg libraries like schedule, APScheduler.
Example.
import schedule, time
from pathlib import Path

def check_file():
    file = Path('somefile.cfg')
    if file.exists() and file.stat().st_size >= 75:
        print('File is ok')
    else:
        print('File not ok')

schedule.every(30).seconds.do(check_file)
while True:
    schedule.run_pending()
    time.sleep(1)
Now most file exists and be at least equal to 75kb or larger.
Test run after a minute i delete some from the file,the status should change.
Output:
λ python check_size.py File is ok File is ok File not ok File not ok
Reply
#3
There is also built-in fcntl — The fcntl and ioctl system calls which contains fcntl.lockf(fd, cmd, len=0, start=0, whence=0). It should be possible to lock file until writing has finished.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  please check this i wanna use a csv file as a graph xCj11 5 1,480 Aug-25-2022, 08:19 PM
Last Post: deanhystad
  Process finished with exit code 137 (interrupted by signal 9: SIGKILL) erdemath 2 9,519 Apr-18-2022, 08:40 PM
Last Post: erdemath
  check if a file exist on the internet and get the size kucingkembar 6 1,759 Apr-16-2022, 05:09 PM
Last Post: kucingkembar
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,931 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  Using .hdf5 files only once they are finished writing pyhill00 7 2,787 Nov-25-2021, 06:01 PM
Last Post: pyhill00
  Check last time file was accessed Pavel_47 4 2,822 Jun-01-2021, 05:47 PM
Last Post: Yoriz
  Check if a file exists. Pedroski55 5 3,299 Sep-08-2020, 10:01 AM
Last Post: Pedroski55
  process finished with exit code -1073741819 (0xC0000005) GMCobraz 8 5,380 Sep-01-2020, 08:19 AM
Last Post: GMCobraz
  How to check to see a dbf file is EOF ? DarkCoder2020 0 1,719 Jun-16-2020, 05:03 PM
Last Post: DarkCoder2020
  Building a script to check size of file upon creation mightyn00b 2 2,384 Apr-04-2020, 04:39 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020