File Permissions

Christos Georgiou tzot at sil-tec.gr
Sat Mar 18 07:51:38 EST 2006


On Fri, 10 Mar 2006 16:43:15 +0200, rumours say that Juho Schultz
<juho.schultz at helsinki.fi> might have written:

>VJ wrote:
>> Hi All
>> 
>> Basically i want to write into a file .If the permissions are not there
>> then print a error message.
>> How do i achive this ???
>> 
>> Thanks,
>> VJ
 
>One way would be a try-except block, and leave the permission checking 
>error message generation, etc. to the operating system.
>
>try:
>     outfile = file(outfilename,"w")
>except IOError, errormsg
>     print "Could not write to file %s: %s" % (outfilename, errormsg)

As a word of caution: the OP is checking for the permissions of an
*existing* file.  Both Juho's and Sybren's suggestions *destroy* the file's
contents.

So, VJ, I'd suggest the following change:

Open the file for read write

	outfile= open(outfilename, "r+b") # I assume binary

and later on catch errors in .write operations.
-- 
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians



More information about the Python-list mailing list