I want to replace the contents of a hidden file, so I attempted to open it in w
mode so it would be erased/truncated:
>>> import os >>> ini_path = '.picasa.ini' >>> os.path.exists(ini_path) True >>> os.access(ini_path, os.W_OK) True >>> ini_handle = open(ini_path, 'w')
But this resulted in a traceback:
IOError: [Errno 13] Permission denied: '.picasa.ini'
However, I was able to achieve the intended result with r+
mode:
>>> ini_handle = open(ini_path, 'r+') >>> ini_handle.truncate() >>> ini_handle.write(ini_new) >>> ini_handle.close()
Q. What is the difference between the w
and r+
modes, such that one has "permission denied" but the other works fine?
UPDATE: I am on win7 x64 using Python 2.6.6, and the target file has its hidden attribute set. When I tried turning off the hidden attribute, w
mode succeeds. But when I turn it back on, it fails again.
Q. Why does w
mode fail on hidden files? Is this known behaviour?
If you get a "permission denied" error when you attempt to run this Python script from the command line, most likely the permissions are wrong. The fix for this problem will depend on how you are calling the script.
It's just how the Win32 API works. Under the hood, Python's open
function is calling the CreateFile
function, and if that fails, it translates the Windows error code into a Python IOError
.
The r+
open mode corresponds to a dwAccessMode
of GENERIC_READ|GENERIC_WRITE
and a dwCreationDisposition
of OPEN_EXISTING
. The w
open mode corresponds to a dwAccessMode
of GENERIC_WRITE
and a dwCreationDisposition
of CREATE_ALWAYS
.
If you carefully read the remarks in the CreateFile
documentation, it says this:
If
CREATE_ALWAYS
andFILE_ATTRIBUTE_NORMAL
are specified,CreateFile
fails and sets the last error toERROR_ACCESS_DENIED
if the file exists and has theFILE_ATTRIBUTE_HIDDEN
orFILE_ATTRIBUTE_SYSTEM
attribute. To avoid the error, specify the same attributes as the existing file.
So if you were calling CreateFile
directly from C code, the solution would be to add in FILE_ATTRIBUTE_HIDDEN
to the dwFlagsAndAttributes
parameter (instead of just FILE_ATTRIBUTE_NORMAL
). However, since there's no option in the Python API to tell it to pass in that flag, you'll just have to work around it by either using a different open mode or making the file non-hidden.
Here are the detailed differences:-
``r'' Open text file for reading. The stream is positioned at the beginning of the file.
``r+'' Open for reading and writing. The stream is positioned at the beginning of the file.
``w'' Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
``w+'' Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
``a'' Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar.
``a+'' Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subse- quent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar.
From python documentation - http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files:-
On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.
So if you are using w
mode, you are actually trying to create a file and you may not have the permissions to do it. r+
is the appropriate choice.
If you are in a situation where you do not yet know where your .picasi.ini
exists or not and your windows user has file creation permissions in that directory and you want to append new information instead of starting at the beginning of the file (a.k.a "append"), then a+
will be the appropriate choice.
It has nothing to do with whether your file is hidden or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With