diff --git a/assessments/prj/carpark-guide.md b/assessments/prj/carpark-guide.md index a3703d4..51a545c 100644 --- a/assessments/prj/carpark-guide.md +++ b/assessments/prj/carpark-guide.md @@ -821,7 +821,7 @@ Create a new local branch named `feature/log-car-activity`. You can do this eith #### Log cars entering and leaving in a file called `log.txt` **Detour – Python file handling:** -Python is a multi-platform language. This means that it can run on different operating systems. However, different operating systems have different ways of representing files and paths. We therefore want to _abstract_ this representation away from our code. We can do this using the `pathlib` module. This module provides a platform-independent way to represent files and paths. We can use it to create a `Path` object representing a file or directory. We can then use this object to create, read, write, and delete files and directories. +Python is a multi-platform language. This means that it can run on different operating systems. However, different operating systems have different ways of representing files and paths. We, therefore, want to _abstract_ this representation away from our code. We can do this using the `pathlib` module. This module provides a platform-independent way to represent files and paths. We can use it to create a `Path` object representing a file or directory. We can then use this object to create, read, write, and delete files and directories. Typically, we import the `Path` class from the `pathlib` module. We can then use the `Path` class to create a `Path` object. For example, `Path("log.txt")` creates a `Path` object that represents a file called `log.txt`. We can then use the `Path` object to create, read, write, and delete files and directories. @@ -862,11 +862,14 @@ When a test creates a file, it is **not** cleaned up automatically. So, we want Path("new_log.txt").unlink(missing_ok=True) ``` --------- -**Bonus:** + +--- + +:bulb: Unlink? What does that mean? It turns out that when you delete files on most operating systems, you unlink the file from a directory entry. The data is still there but can now be overwritten. When we program, we often use the more precise and explicit terms. --------- + +--- Notice how we have inadvertently made our test code hard to maintain (if we change the log file's name, we have to change it in two places). Can you think of a way to improve this code? Hint: consider using a class attribute or new instance variable in the `setUp` method.