|
245 | 245 | "\n", |
246 | 246 | "Files on non-volatile storage media are organized by a set of rules known as a **file system**. File systems are made up of files and **directories**, which are containers for both files and other directories.\n", |
247 | 247 | "\n", |
248 | | - "By default, when we create a new file by opening it goes in the current directory (wherever we were when we ran the program). Similarly, when we open a file for reading, Python looks for it in the current directory. In the above example, we have used `os.path.join()` to write the file to a specific directory rather than the directory we are running this notebook from. If `'test.txt'` was used in the `open` statement, the file would have been written to the current working directory. The current working directory can be determined using:" |
| 248 | + "By default, when we create a new file by opening it goes in the current directory (wherever we were when we ran the program). Similarly, when we open a file for reading, Python looks for it in the current directory. In the above example, we have used `pathlib.Path` to write the file to a specific directory rather than the directory we are running this notebook from. If `'test.txt'` was used in the `open` statement, the file would have been written to the current working directory. The current working directory can be determined using:" |
249 | 249 | ] |
250 | 250 | }, |
251 | 251 | { |
|
255 | 255 | "metadata": {}, |
256 | 256 | "outputs": [], |
257 | 257 | "source": [ |
258 | | - "# The os module used to be the preferred way to access\n", |
259 | | - "# the current working directory; now we use pathlib\n", |
260 | | - "# os.getcwd()\n", |
| 258 | + "# the pathlib Path object can be used to find the current working directory\n", |
261 | 259 | "\n", |
262 | 260 | "cwd = Path.cwd()\n", |
263 | 261 | "print(cwd)" |
|
268 | 266 | "id": "3a60f550", |
269 | 267 | "metadata": {}, |
270 | 268 | "source": [ |
271 | | - "Determine the directory that we wrote `'test.txt'` (`fname`) to in the blank code block below using `os.path.abspath()`:" |
| 269 | + "Determine the directory that we wrote `'test.txt'` (`fname`) to in the blank code block below using `<Path>.resolve()` where `Path` indicates the `pathlib.Path` object:" |
272 | 270 | ] |
273 | 271 | }, |
274 | 272 | { |
|
278 | 276 | "metadata": {}, |
279 | 277 | "outputs": [], |
280 | 278 | "source": [ |
281 | | - "# os.path.abspath(fname)\n", |
282 | 279 | "print(fname)\n", |
283 | 280 | "\n", |
284 | 281 | "# to get the absolute path using pathlib, we use .resolve()\n", |
|
296 | 293 | "\n", |
297 | 294 | "`os.path` includes a number of useful methods for manipulating pathnames. For example, `os.path.normpath(path)` can be used to take Unix style paths into paths that can be used with Windows.\n", |
298 | 295 | "\n", |
299 | | - "Take a look at https://docs.python.org/3.9/library/os.path.html for more information of `os.path` methods." |
| 296 | + "Take a look at https://docs.python.org/3.9/library/os.path.html for more information on `os.path` methods or https://docs.python.org/3/library/pathlib.html for more information on `pathlib.Path`.\n", |
| 297 | + "\n", |
| 298 | + "Alternatively, `pathlib.Path` stores and operates on paths in a operating-system-agnostic way." |
300 | 299 | ] |
301 | 300 | }, |
302 | 301 | { |
|
0 commit comments