diff --git a/notebooks/part0_python_intro/04_files_and_strings.ipynb b/notebooks/part0_python_intro/04_files_and_strings.ipynb index d08075e3..710728b5 100644 --- a/notebooks/part0_python_intro/04_files_and_strings.ipynb +++ b/notebooks/part0_python_intro/04_files_and_strings.ipynb @@ -245,7 +245,7 @@ "\n", "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", "\n", - "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:" + "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:" ] }, { @@ -255,9 +255,7 @@ "metadata": {}, "outputs": [], "source": [ - "# The os module used to be the preferred way to access\n", - "# the current working directory; now we use pathlib\n", - "# os.getcwd()\n", + "# the pathlib Path object can be used to find the current working directory\n", "\n", "cwd = Path.cwd()\n", "print(cwd)" @@ -268,7 +266,7 @@ "id": "3a60f550", "metadata": {}, "source": [ - "Determine the directory that we wrote `'test.txt'` (`fname`) to in the blank code block below using `os.path.abspath()`:" + "Determine the directory that we wrote `'test.txt'` (`fname`) to in the blank code block below using `.resolve()` where `Path` indicates the `pathlib.Path` object:" ] }, { @@ -278,7 +276,6 @@ "metadata": {}, "outputs": [], "source": [ - "# os.path.abspath(fname)\n", "print(fname)\n", "\n", "# to get the absolute path using pathlib, we use .resolve()\n", @@ -296,7 +293,9 @@ "\n", "`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", "\n", - "Take a look at https://docs.python.org/3.9/library/os.path.html for more information of `os.path` methods." + "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", + "\n", + "Alternatively, `pathlib.Path` stores and operates on paths in a operating-system-agnostic way." ] }, { diff --git a/notebooks/part0_python_intro/solutions/04_files_and_strings.ipynb b/notebooks/part0_python_intro/solutions/04_files_and_strings.ipynb index f871c91e..6c889be4 100644 --- a/notebooks/part0_python_intro/solutions/04_files_and_strings.ipynb +++ b/notebooks/part0_python_intro/solutions/04_files_and_strings.ipynb @@ -339,7 +339,7 @@ "\n", "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", "\n", - "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:" + "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:" ] }, { @@ -357,9 +357,7 @@ } ], "source": [ - "# The os module used to be the preferred way to access\n", - "# the current working directory; now we use pathlib\n", - "# os.getcwd()\n", + "# the pathlib Path object can be used to find the current working directory\n", "\n", "cwd = Path.cwd()\n", "print(cwd)" @@ -370,7 +368,7 @@ "id": "3a60f550", "metadata": {}, "source": [ - "Determine the directory that we wrote `'test.txt'` (`fname`) to in the blank code block below using `os.path.abspath()`:" + "Determine the directory that we wrote `'test.txt'` (`fname`) to in the blank code block below using `.resolve()` where `Path` indicates the `pathlib.Path` object:" ] }, { @@ -389,7 +387,6 @@ } ], "source": [ - "# os.path.abspath(fname)\n", "print(fname)\n", "\n", "# to get the absolute path using pathlib, we use .resolve()\n", @@ -407,7 +404,9 @@ "\n", "`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", "\n", - "Take a look at https://docs.python.org/3.9/library/os.path.html for more information of `os.path` methods." + "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", + "\n", + "Alternatively, `pathlib.Path` stores and operates on paths in a operating-system-agnostic way." ] }, {