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

前端 未结 4 573
暗喜
暗喜 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:18

    This is not quite what you want, since it just tests whether a given file is write-able. But in case it's helpful:

    import os
    
    filename = "a.txt"
    if not os.access(filename, os.W_OK):
        print "Write access not permitted on %s" % filename
    

    (I'm not aware of any platform-independent way to do what you ask)

提交回复
热议问题