Skip to content

Commit 7193cd4

Browse files
committed
docs
1 parent 48acb1f commit 7193cd4

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

docs/source/guide.rst

+20-12
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ Why use PyFilesystem?
88

99
If you are comfortable using the Python standard library, you may be wondering; *why learn another API for working with files?*
1010

11-
The PyFilesystem API is generally simpler than the ``os`` and ``io`` modules. There are fewer ways to shoot yourself in the foot, the code tends to be more terse, and (arguably) more readable. This may be reason alone to use it, but there are other compelling reasons you should use ``import fs`` for even straightforward filesystem code.
11+
The PyFilesystem API is generally simpler than the ``os`` and ``io`` modules -- there are fewer edge cases and less ways to shoot yourself in the foot. This may be reason alone to use it, but there are other compelling reasons you should use ``import fs`` for even straightforward filesystem code.
1212

1313
The abstraction offered by FS objects means that you can write code that is agnostic to where your files are physically located. For instance, if you wrote a function that searches a directory for duplicates files, it will work unaltered with a directory on your hard-drive, or in a zip file, on an FTP server, on Amazon S3, etc.
1414

1515
As long as an FS object exists for your chosen filesystem (or any data store that resembles a filesystem), you can use the same API. This means that you can defer the decision regarding where you store data to later. If you decide to store configuration in the *cloud*, it could be a single line change and not a major refactor.
1616

17+
1718
PyFilesystem can also be beneficial for unit-testing; by swapping the OS filesystem with an in-memory filesystem, you can write tests without having to manage (or mock) file IO. And you can be sure that your code will work on Linux, MacOS, and Windows.
1819

1920
Opening Filesystems
@@ -84,7 +85,7 @@ This can be a useful debugging aid!
8485
Closing Filesystems
8586
~~~~~~~~~~~~~~~~~~~
8687

87-
FS objects have a ``close`` method (:meth:`fs.base.FS.close`) which will perform any required clean-up actions. For many filesystems (notably :class:`fs.osfs.OSFS`), the ``close`` method does very little, however, other filesystems may only finalize files or release resources once ``close()`` is called.
88+
FS objects have a :meth:`fs.base.FS.close` methd which will perform any required clean-up actions. For many filesystems (notably :class:`fs.osfs.OSFS`), the ``close`` method does very little. Other filesystems may only finalize files or release resources once ``close()`` is called.
8889

8990
You can call ``close`` explicitly once you are finished using a filesystem. For example::
9091

@@ -102,20 +103,23 @@ Using FS objects as a context manager is recommended as it will ensure every FS
102103
Directory Information
103104
~~~~~~~~~~~~~~~~~~~~~
104105

105-
Filesystem objects have a ``listdir`` method which is similar to ``os.listdir``; it takes a path to a directory and returns a list of file names. Here's an example::
106+
Filesystem objects have a :meth:`fs.base.FS.listdir` method which is similar to ``os.listdir``; it takes a path to a directory and returns a list of file names. Here's an example::
106107

107108
>>> home_fs.listdir('/projects')
108109
['fs', 'moya', 'README.md']
109110

110-
An alternative method exists for listing directories; if you call :meth:`fs.base.FS.scandir` it will return an *iterable* of :ref:`info` objects. Here's an example::
111+
An alternative method exists for listing directories; :meth:`fs.base.FS.scandir` returns an *iterable* of :ref:`info` objects. Here's an example::
111112

112113
>>> directory = list(home_fs.scandir('/projects'))
113114
>>> directory
114115
[<dir 'fs'>, <dir 'moya'>, <file 'README.md'>]
115116

116-
Info objects have a number of advantages over just a filename. For instance, you can know if a name references a directory with :attr:`fs.info.Info.is_dir`. Otherwise you would need to call :meth:`fs.base.FS.isdir` for each name in the directory, which may involve additional system calls (or request in the case of a network filesystem).
117+
Info objects have a number of advantages over just a filename. For instance you can tell if an info object references a file or a directory with the :attr:`fs.info.Info.is_dir` attribute, without an additional system call. Info objects may also contain information such as size, modified time, etc. if you request it in the ``namespaces`` parameter.
118+
117119

118-
The reason that ``scandir`` returns an iterable rather than a list, is that it can be more efficient to retrieve directory information in chunks if the directory is very large, or if the information must be retrieved over a network.
120+
.. note::
121+
122+
The reason that ``scandir`` returns an iterable rather than a list, is that it can be more efficient to retrieve directory information in chunks if the directory is very large, or if the information must be retrieved over a network.
119123

120124
Additionally, FS objects have a :meth:`fs.base.FS.filterdir` method which extends ``scandir`` with the ability to filter directory contents by wildcard(s). Here's how you might find all the Python files in a directory:
121125

@@ -165,23 +169,21 @@ Walking
165169

166170
Often you will need to scan the files in a given directory, and any sub-directories. This is known as *walking* the filesystem.
167171

168-
Here's how you would print the paths to all your Python files in your home-directory (and sub-directories)::
172+
Here's how you would print the paths to all your Python files in your home directory::
169173

170174
>>> from fs import open_fs
171175
>>> home_fs = open_fs('~/')
172176
>>> for path in home_fs.walk.files(wildcards=['*.py']):
173177
... print(path)
174178

175-
This might take a while to run if you have a lot of Python code!
176-
177-
The ``walk`` attribute on FS objects is instance of a :class:`fs.walk.BoundWalker`, which should be able to handle most directory walking requirements. It is designed to be customizable, if you do need to further tune the directory walk.
179+
The ``walk`` attribute on FS objects is instance of a :class:`fs.walk.BoundWalker`, which should be able to handle most directory walking requirements.
178180

179181
See :ref:`walking` for more information on walking directories.
180182

181183
Working with Files
182184
~~~~~~~~~~~~~~~~~~
183185

184-
You can open a file from a FS object with :meth:`fs.base.FS.open`, which is very similar to ``io.open`` in the standard library. Here's how you might open a file called reminder.txt in your home directory::
186+
You can open a file from a FS object with :meth:`fs.base.FS.open`, which is very similar to ``io.open`` in the standard library. Here's how you might open a file called "reminder.txt" in your home directory::
185187

186188
>>> with open_fs('~/') as home_fs:
187189
... with home_fs.open('reminder.txt') as reminder_file:
@@ -190,7 +192,7 @@ You can open a file from a FS object with :meth:`fs.base.FS.open`, which is very
190192

191193
In the case of a ``OSFS``, a standard file-like object will be returned. Other filesystems may return a different object supporting the same methods. For instance, :class:`fs.memoryfs.MemoryFS` will return a ``io.BytesIO`` object.
192194

193-
PyFilesystem also offers a number of shortcuts for common file related operations. For example, :meth:`fs.base.FS.getbytes` will return the file contents as a bytes object, and :meth:`fs.base.FS.gettext` will read unicode text. Using these methods is generally preferable to explicitly opening files, because the FS object may have an optimized implementation.
195+
PyFilesystem also offers a number of shortcuts for common file related operations. For example, :meth:`fs.base.FS.getbytes` will return the file contents as a bytes, and :meth:`fs.base.FS.gettext` will read unicode text. Using these methods is generally preferable to explicitly opening files, as the FS object may have an optimized implementation.
194196

195197
Other *shortcut* methods are :meth:`fs.base.FS.setbin`, :meth:`fs.base.FS.setbytes`, :meth:`fs.base.FS.settext`.
196198

@@ -213,3 +215,9 @@ Which is the equivalent to this, more verbose, code::
213215
>>> from fs.zipfs import ZipFS
214216
>>> copy_fs(OSFS('~/projects'), ZipFS('projects.zip'))
215217

218+
The :func:`fs.copy.copy_fs` and :func:`fs.copy.copy_dir` functions also accept a :class:`fs.walk.Walker` parameter, which can you use to filter the files that will be copied. For instance, if you only wanted back up your python files, you could use something like this::
219+
220+
>>> from fs.copy import copy_fs
221+
>>> from fs.walk import Walker
222+
>>> copy_fs('~/projects', 'zip://projects.zip', walker=Walker(wildcards=['*.py']))
223+

docs/source/info.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Here's an example of retrieving file information::
1818
>>> from fs.osfs import OSFS
1919
>>> fs = OSFS('.')
2020
>>> fs.settext('example.txt', 'Hello, World!')
21-
>>> info = fs.getinfo('example.txt', 'details')
21+
>>> info = fs.getinfo('example.txt', namespaces=['details'])
2222
>>> info.name
2323
'example.txt'
2424
>>> info.is_dir

docs/source/reference.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Reference
66

77
reference/base.rst
88
reference/copy.rst
9-
reference/errors.rst
109
reference/enums.rst
10+
reference/errors.rst
1111
reference/info_objects.rst
1212
reference/move.rst
1313
reference/opener.rst
1414
reference/path.rst
1515
reference/tree.rst
16-
reference/walk.rst
16+
reference/walk.rst

fs/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def getinfo(self, path, namespaces=None):
8484

8585
def listdir(self, path):
8686
"""
87-
Get an iterator of the resource names in a directory.
87+
Get a list of the resource names in a directory.
8888
8989
:param str path: A path to a directory on the filesystem.
9090
:return: list of names, relative to ``path``.

0 commit comments

Comments
 (0)