[Tutor] os.name to check before file locking

Sheila King sheila@thinkspot.net
Fri, 24 Aug 2001 12:07:34 -0700

I'm working on writing a cross-platform file locking object to wrap file
locking calls on posix and windows platforms. (It really is a big
complaint of mine about the lack of support for file locking in Python,
but anyhow, I'm just going to deal with it and write my own...)

I'm using, for the posix platforms, Mark Lutz' examples from Programming
Python 2nd ed, Chapter 14 as a guide. Very nice stuff he has there.

I think I've figured out how to use the msvcrt module to lock files for
Windows machines.

And so, here's my question:

I will have to test for what platform/os my code is running on, in order
to know which libraries to use (i.e. fcntl or msvcrt).

I notice that in the os module, we have the os.name command, which
(according to the docs) returns one of the following:
'posix', 'nt', 'dos', 'mac', 'os2', 'ce', 'java'

'nt' is for any windows, including win98 and so on.
'posix' is for all unix type machines.

Alternatively, under the sys module, there is the command sys.platform.
The docs don't give a complete list of what output this might produce,
but a sample:
'sunos5' or 'linux1'. This can be used to append platform-specific
components to path, for instance.

I think that all I will need to use here, is os.name, to check for which
module to use. Let's face it, if fcntl won't do what I need on the
Unix-type platforms, then I really have no idea how to handle it, so my
code simply won't run on such a machine.

I guess I'm just trying to get some advice, or make sure that I only
want to use os.name and not mess with sys.platform.

Sheila King