Skip to content

Commit 8dba448

Browse files
authored
Merge pull request #187 from mnfienen/main
refactored path discussion in intro to python 04 to use pathlib
2 parents 1b38537 + 31fc2fd commit 8dba448

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

notebooks/part0_python_intro/04_files_and_strings.ipynb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@
245245
"\n",
246246
"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",
247247
"\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:"
249249
]
250250
},
251251
{
@@ -255,9 +255,7 @@
255255
"metadata": {},
256256
"outputs": [],
257257
"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",
261259
"\n",
262260
"cwd = Path.cwd()\n",
263261
"print(cwd)"
@@ -268,7 +266,7 @@
268266
"id": "3a60f550",
269267
"metadata": {},
270268
"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:"
272270
]
273271
},
274272
{
@@ -278,7 +276,6 @@
278276
"metadata": {},
279277
"outputs": [],
280278
"source": [
281-
"# os.path.abspath(fname)\n",
282279
"print(fname)\n",
283280
"\n",
284281
"# to get the absolute path using pathlib, we use .resolve()\n",
@@ -296,7 +293,9 @@
296293
"\n",
297294
"`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",
298295
"\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."
300299
]
301300
},
302301
{

notebooks/part0_python_intro/solutions/04_files_and_strings.ipynb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@
339339
"\n",
340340
"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",
341341
"\n",
342-
"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:"
342+
"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:"
343343
]
344344
},
345345
{
@@ -357,9 +357,7 @@
357357
}
358358
],
359359
"source": [
360-
"# The os module used to be the preferred way to access\n",
361-
"# the current working directory; now we use pathlib\n",
362-
"# os.getcwd()\n",
360+
"# the pathlib Path object can be used to find the current working directory\n",
363361
"\n",
364362
"cwd = Path.cwd()\n",
365363
"print(cwd)"
@@ -370,7 +368,7 @@
370368
"id": "3a60f550",
371369
"metadata": {},
372370
"source": [
373-
"Determine the directory that we wrote `'test.txt'` (`fname`) to in the blank code block below using `os.path.abspath()`:"
371+
"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:"
374372
]
375373
},
376374
{
@@ -389,7 +387,6 @@
389387
}
390388
],
391389
"source": [
392-
"# os.path.abspath(fname)\n",
393390
"print(fname)\n",
394391
"\n",
395392
"# to get the absolute path using pathlib, we use .resolve()\n",
@@ -407,7 +404,9 @@
407404
"\n",
408405
"`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",
409406
"\n",
410-
"Take a look at https://docs.python.org/3.9/library/os.path.html for more information of `os.path` methods."
407+
"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",
408+
"\n",
409+
"Alternatively, `pathlib.Path` stores and operates on paths in a operating-system-agnostic way."
411410
]
412411
},
413412
{

0 commit comments

Comments
 (0)