Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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"
like image 612
Hailiang Zhang Avatar asked Nov 22 '11 18:11

Hailiang Zhang


1 Answers

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)

like image 127
Ben Hoffstein Avatar answered Oct 16 '22 22:10

Ben Hoffstein