Python script to check if file is locked also display last modified time

To lock file for testing use this command 

The below command will lock /home/ubuntu/fileloc

flock -x -w 5 /home/ubuntu/fileloc echo "4" >  /home/ubuntu/fileloc && sleep 5

------------------------
test.py
------------------------
import os, time
import datetime as dt

def is_locked(filepath):
   
    locked = None
    file_object = None
    if os.path.exists(filepath):
        try:
            print "Trying to open %s." % filepath
            buffer_size = 8
            # Opening file in append mode and read the first 8 characters.
            file_object = open(filepath, 'a', buffer_size)
            if file_object:
                print "%s is not locked." % filepath
                locked = False
        except IOError, message:
            print "File is locked (unable to open in append mode). %s." % \
                  message
            locked = True
        finally:
            if file_object:
                file_object.close()
                print "%s closed." % filepath
    else:
        print "%s not found." % filepath
    return locked

def wait_for_files(filepaths):
   

    for filepath in filepaths:
      
       if is_locked(filepath):
            print "%s is currently in use." % \
                  (filepath)
            st = os.stat(filepath)    
            mtime = dt.datetime.fromtimestamp(st.st_mtime)
            print('%s modified %s'%(filepath, mtime))

                

# Test
if __name__ == '__main__':
    files = [r"/home/ubuntu/filelock"]
    print wait_for_files(files)
-------------------------

python test.py

12 comments:

soumya said...

This is an excellent blog, Thanks for sharing most valuable information with us. keep share more on Devops Online Training Bangalore

Ajith said...

Great Article, thanks for sharing the valuable information!

gowsalya said...

This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
msbi online training

rithiaanandh said...

Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such an awesome article.
Sql server dba online training

Merlin Kristianti said...

Nah, buat kalian seseorang pemain judi pemula yang barusan bermain game domino online indonesia. Berikut ini ialah panduang untuk memenangi permainan kartu online yang tengah di lakukan
asikqq
dewaqq
sumoqq
interqq
pionpoker
bandar ceme terpercaya
hobiqq
paito warna
forum prediksi

Kiruthiprabha said...

Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision...
Oracle DBA Online Training

Nandhini said...

Thank you so much for sharing this amazing article with us. I will stay connected with your blogs for future posts.
Python training in bangalore
Python training in Bangalore

Nandhini said...
This comment has been removed by the author.
Nandhini said...

Thanks for sharing like a wonderful blog’s learn more new information from your blog. Keep sharing the post like this…

Python training in bangalore
Python training in Bangalore
Data science with python training in Bangalore
Angular js training in bangalore
Hadoop training in bangalore
DevOPs training in bangalore
Agile and scrum training in bangalore

Praneeth M said...
This comment has been removed by the author.
lavanya said...

The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.excellent


Java training in Chennai

Java training in Bangalore

Java training in Hyderabad

Java Training in Coimbatore

Java Online Training

iteducationcentre said...

Superb post. It was worth reading the blog.
visit Python Course in Pune