You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/guide.rst
+29-28
Original file line number
Diff line number
Diff line change
@@ -57,33 +57,33 @@ The opener system is particularly useful when you want to store the physical loc
57
57
If you don't specify the protocol in the FS URL, then PyFilesystem will assume you want a OSFS relative from the current working directory. So the following would be an equivalent way of opening your home directory::
Calling :meth:`fs.base.FS.tree` on a FS object will print an ascii tree view of your filesystem. Here's an example::
68
68
69
69
>>> from fs import open_fs
70
70
>>> my_fs = open_fs('.')
71
71
>>> my_fs.tree()
72
72
├── locale
73
-
│ ╰── readme.txt
73
+
│ └── readme.txt
74
74
├── logic
75
75
│ ├── content.xml
76
76
│ ├── data.xml
77
77
│ ├── mountpoints.xml
78
-
│ ╰── readme.txt
78
+
│ └── readme.txt
79
79
├── lib.ini
80
-
╰── readme.txt
80
+
└── readme.txt
81
81
82
82
This can be a useful debugging aid!
83
83
84
84
85
-
Closing Filesystems
86
-
~~~~~~~~~~~~~~~~~~~
85
+
Closing
86
+
~~~~~~~
87
87
88
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.
89
89
@@ -124,11 +124,11 @@ Info objects have a number of advantages over just a filename. For instance you
124
124
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:
By default, the resource information objects returned by ``scandir`` and ``listdir`` will contain only the file name and the ``is_dir`` flag. You can request additional information with the ``namespaces`` parameter. Here's how you can request additional details (such as file size and file modified times)::
This will add a ``size`` and ``modified`` property (and others) to the resource info objects. Which makes code such as this work::
134
134
@@ -164,22 +164,6 @@ The :class:`fs.base.FS.makedir` and :class:`fs.base.FS.makedirs` methods also re
164
164
165
165
Working with ``SubFS`` objects means that you can generally avoid writing much path manipulation code, which tends to be error prone.
166
166
167
-
Walking
168
-
~~~~~~~
169
-
170
-
Often you will need to scan the files in a given directory, and any sub-directories. This is known as *walking* the filesystem.
171
-
172
-
Here's how you would print the paths to all your Python files in your home directory::
173
-
174
-
>>> from fs import open_fs
175
-
>>> home_fs = open_fs('~/')
176
-
>>> for path in home_fs.walk.files(wildcards=['*.py']):
177
-
... print(path)
178
-
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.
180
-
181
-
See :ref:`walking` for more information on walking directories.
182
-
183
167
Working with Files
184
168
~~~~~~~~~~~~~~~~~~
185
169
@@ -192,10 +176,26 @@ You can open a file from a FS object with :meth:`fs.base.FS.open`, which is very
192
176
193
177
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.
194
178
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.
179
+
PyFilesystem also offers a number of shortcuts for common file related operations. For instance, :meth:`fs.base.FS.getbytes` will return the file contents as a bytes, and :meth:`fs.base.FS.gettext` will read unicode text. These methods is generally preferable to explicitly opening files, as the FS object may have an optimized implementation.
196
180
197
181
Other *shortcut* methods are :meth:`fs.base.FS.setbin`, :meth:`fs.base.FS.setbytes`, :meth:`fs.base.FS.settext`.
198
182
183
+
Walking
184
+
~~~~~~~
185
+
186
+
Often you will need to scan the files in a given directory, and any sub-directories. This is known as *walking* the filesystem.
187
+
188
+
Here's how you would print the paths to all your Python files in your home directory::
189
+
190
+
>>> from fs import open_fs
191
+
>>> home_fs = open_fs('~/')
192
+
>>> for path in home_fs.walk.files(filter=['*.py']):
193
+
... print(path)
194
+
195
+
The ``walk`` attribute on FS objects is instance of a :class:`fs.walk.BoundWalker`, which should be able to handle most directory walking requirements.
196
+
197
+
See :ref:`walking` for more information on walking directories.
198
+
199
199
Moving and Copying
200
200
~~~~~~~~~~~~~~~~~~
201
201
@@ -219,5 +219,6 @@ The :func:`fs.copy.copy_fs` and :func:`fs.copy.copy_dir` functions also accept a
0 commit comments