20

I have noticed that, in addition to the documented mode characters, Python 2.7.5.1 in Windows XP and 8.1 also accepts modes U and D at least when reading files. Mode U is used in numpy's genfromtxt. Mode D has the effect that the file is deleted, as per the following code fragment:

 f = open('text.txt','rD')
 print(f.next())
 f.close()  # file text.txt is deleted when closed

Does anybody know more about these modes, especially whether they are a permanent feature of the language applicable also on Linux systems?

5
  • 3
    open('text.txt','rD') for an existing file on Linux does not raise any errors for me, although the file is not deleted when closed. I am really surprised that if true, this behavior is not documented. U flag is documented though. Commented Jul 19, 2015 at 14:55
  • 2
    Just replicated this on Windows. This is very odd indeed. Commented Jul 19, 2015 at 15:03
  • 3
    I made the same mistake a moment ago. The mention is here. In the fileobject and not the io. Commented Jul 19, 2015 at 15:52
  • @tmr232 will you please give python and os version? what mistake are you referring to? Commented Jul 19, 2015 at 15:53
  • 1
    Windows 8, Python 2.7.10 32bit. Commented Jul 19, 2015 at 15:53

2 Answers 2

8

The D flag seems to be Windows specific. Windows seems to add several flags to the fopen function in its CRT, as described here.

While Python does filter the mode string to make sure no errors arise from it, it does allow some of the special flags, as can be seen in the Python sources here. Specifically, it seems that the N flag is filtered out, while the T and D flags are allowed:

while (*++mode) {
    if (*mode == ' ' || *mode == 'N') /* ignore spaces and N */
        continue;
    s = "+TD"; /* each of this can appear only once */
    ...

I would suggest sticking to the documented options to keep the code cross-platform.

Sign up to request clarification or add additional context in comments.

2 Comments

Also see the documentation on the Windows CreateFile function.
"I would suggest sticking to the documented options" yay! See also module tempfile
5

This is a bit misleading. open() as mode arg accepts any character, while you pass a valid one i.e.: "w,r,b,+,a".

Thus you can write: open("fname", "w+ANYTHINGYOUWANT"). It will open file as open("fname", "w+"). And open("fname", "rANYTHINGYOUWANT"). will open file as open("fname", "r").

Regarding "U" flag:

In addition to the standard fopen() values mode may be 'U' or 'rU'. Python is usually built with universal newlines support; supplying 'U' opens the file as a text file, but lines may be terminated by any of the following: the Unix end-of-line convention '\n', the Macintosh convention '\r', or the Windows convention '\r\n'. All of these external representations are seen as '\n' by the Python program. If Python is built without universal newlines support a mode with 'U' is the same as normal text mode. Note that file objects so opened also have an attribute called newlines which has a value of None (if no newlines have yet been seen), '\n', '\r', '\r\n', or a tuple containing all the newline types seen.

As you can read in Python documentation https://docs.python.org/2/library/functions.html#open

EDIT:

D: Specifies a file as temporary. It is deleted when the last file pointer is closed.

as you can read in @tmr232's link.

The c, n, t, S, R, T, and D mode options are Microsoft extensions for fopen and _fdopen and should not be used where ANSI portability is desired

Further update:

I propose to submit the phenomenon as a bug, because opening a file as read only i.e. with flag "r", then allowing to delete after/via closing it adding a single character like "D", even accidentally is a serious security issue, I think.

But, if this has some unavoidable functionality, please inform me.

6 Comments

I could not be able to reproduce that, but maybe this is some other latent file problem, I suppose?
@rth it was reproduced see tmr232 response above, will you please give a reference for U documentation?
@GyörgySolymosi will you please give python and os version
@user2422503 Python 2.7.9 under Linux
Yeah, it definitely deletes when using open(..., 'rD') as well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.