os.stat on Windows "nul" file
Asked Answered
A

2

5

Why can't I call os.stat on the special Windows file nul?

>>> import os
>>> os.stat('nul')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
WindowsError: [Error 87] The parameter is incorrect: 'nul'

I can open it:

>>> f = open('nul', 'r')
>>> f.read(10)
''

I was hoping to check for special files like /dev/null and nul in a cross-platform way with stat.S_ISCHR and was surprised to find that I can't stat a file that I can open.

Arnst answered 5/12, 2012 at 1:50 Comment(0)
P
5

According to this old Python bug, it's supposed to be that way:

http://bugs.python.org/issue1311

Presumably answered 5/12, 2012 at 2:17 Comment(0)
R
2

This is the behavior of the underlying Win32 GetFileAttributesEx() and CreateFile() functions, which are eventually called by os.stat() and open().

Rees answered 5/12, 2012 at 2:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.