summaryrefslogtreecommitdiff
path: root/docs/base.rst
blob: 08e45438d914242fbc23a5d8920c2a78a73a7954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
fs.base
=======

This module contains the basic FS interface and a number of other essential interfaces.

fs.base.FS
----------

All Filesystem objects inherit from this class.

.. autoclass:: fs.base.FS
    :members:

fs.base.SubFS
-------------

A SubFS is an FS implementation that represents a directory on another Filesystem. When you use the :meth:`~fs.base.FS.opendir` method it will return a SubFS instance. You should not need to instantiate a SubFS directly.

For example::

    from fs.osfs import OSFS
    home_fs = OSFS('foo')
    bar_fs = home_fs.opendir('bar')


fs.base.NullFile
----------------

A NullFile is a file-like object with no functionality. It is used in situations where a file-like object is required but the caller doesn't have any data to read or write.

The :meth:`~fs.base.FS.safeopen` method returns an NullFile instance, which can reduce error-handling code.

For example, the following code may be written to append some text to a log file::

    logfile = None
    try:
        logfile = myfs.open('log.txt', 'a')
        logfile.writeline('operation successful!')
    finally:
        if logfile is not None:
            logfile.close()

This could be re-written using the `safeopen` method::

    myfs.safeopen('log.txt', 'a').writeline('operation successful!')

If the file doesn't exist then the call to writeline will be a null-operation (i.e. not do anything).