How to check whether a file is_open and the open_status in python

前端 未结 4 535
暗喜
暗喜 2021-01-12 07:39

Is there any python functions such as:

filename = \"a.txt\"
if is_open(filename) and open_status(filename)==\'w\':
   print filename,\" is open for writing\"         


        
4条回答
  •  悲哀的现实
    2021-01-12 08:19

    I think the best way to do that is to try.

    def is_already_opened_in_write_mode(filename)
         if os.path.exists(filename):
             try:
                 f = open(filename, 'a')
                 f.close()
             except IOError:
                 return True
         return False
    

提交回复
热议问题