5

I was trying to use the os.mknod function in Python 3.5.0 in Windows 7, however I find the error:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    os.mknod
AttributeError: module 'os' has no attribute 'mknod'

I guess it's supposed to be there, since https://docs.python.org/3/library/os.html doesn't say anything about limited availability. Is there another option to use for a similar function in Windows? I'm just looking to create an empty file in a specific path, and I was thinking calling open(path, 'w') is kinda ugly for this.

I don't know if this might be a version specific problem since I've never used Python in Windows before.

spalac24
  • 1,076
  • 1
  • 7
  • 16
  • 1
    This question was [asked before](http://stackoverflow.com/questions/32691981/pythonmodule-os-has-no-attribute-mknod) but there was no answer. – BrenBarn Oct 29 '15 at 18:15
  • Yes, I found it :( However I was trying to give a bit more information about my specific issue (OS, version and such) and maybe asking for an alternative. – spalac24 Oct 29 '15 at 18:17
  • There's no direct equivalent for [POSIX `mknod`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mknod.html) on Windows. Devices use the ``\\.\`` prefix. Device names are created per logon via [`DefineDosDevice`](https://msdn.microsoft.com/en-us/library/aa363904), but the actual device object target is created by the driver. For example, `\\.\nul` links to native `\Device\Null`. – Eryk Sun Oct 29 '15 at 21:51
  • Named pipes are created via [`CreateNamedPipe`](https://msdn.microsoft.com/en-us/library/aa365150) and accessed via the NamedPipe filesystem, `\\.\pipe` (native `\Device\NamedPipe`). Filesystem directories are created via [`CreateDirectory`](https://msdn.microsoft.com/en-us/library/aa363855). Regular files are created and opened via [`CreateFile`](https://msdn.microsoft.com/en-us/library/aa363858), which can also open devices prefixed by ``\\.\``. – Eryk Sun Oct 29 '15 at 21:58
  • So, that basically means there's no portable way to create an empty file other tan `open`? – spalac24 Oct 30 '15 at 19:45

2 Answers2

1

Since commit in 2016, this is now documented:

Availability: Unix.

Zombo
  • 1
  • 62
  • 391
  • 407
0

I'm new here and on Python World (although learning kind of quickly...), and just stumbled uppon the same issue.

My suggestion: for now, I would just go with the following and turn a blind eye on it...

with open('name_your_file.extention', 'w') as an_alias_for_it:
pass

In the end, it's not neat, but will be naturally "portable" among POSIX and NT systems.