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/Secure-Coding-Guide-for-Python/CWE-703/CWE-755/README.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,9 +17,9 @@ The full list of OS exceptions can be found in the Python documentation [[Python
17
17
18
18
It is important to handle those exceptions when performing file I/O operations.
19
19
20
-
## Non-Compliant Code Example (os.remove()/os.unlink())
20
+
## Non-Compliant Code Example (`os.remove()`/`os.unlink()`)
21
21
22
-
This code example demonstrates an attempt to read a non-existent file using the os module. The `read_file()` function opens a file and reads its content using `os.open()` and `os.read()`. If the file does not exist, an `OSError` or `FileNotFoundError` will be raised when trying to access the randomly generated file name.
22
+
This code example demonstrates an attempt to read a non-existent file using the `os` module. The `read_file()` function opens a file and reads its content using `os.open()` and `os.read()`. If the file does not exist, an `OSError` or `FileNotFoundError` will be raised when trying to access the randomly generated file name.
23
23
24
24
*[noncompliant01.py](noncompliant01.py):*
25
25
@@ -44,7 +44,7 @@ def read_file(file):
44
44
read_file(f"{uuid.uuid4()}.txt")
45
45
```
46
46
47
-
## Compliant Solution (try/except blocks)
47
+
## Compliant Solution (`try/except` blocks)
48
48
49
49
The file opening and reading should be surrounded by the `try/except` block. This way, we can catch the generic `OSError` and handle the error differently depending on its cause (such as the file not existing or it being a directory instead).
50
50
@@ -82,7 +82,7 @@ def read_file(file):
82
82
read_file(f"{uuid.uuid4()}.txt")
83
83
```
84
84
85
-
## Non-Compliant Code Example (pathlib.Path.unlink())
85
+
## Non-Compliant Code Example (`pathlib.Path.unlink()`)
86
86
87
87
The `pathlib` module also provides functions for opening and reading files. The `Path.read_text()` method attempts to read the content of the file represented by the `Path` object. If the file does not exist, it will raise a `FileNotFoundError`. In this code example, this exception is expected when attempting to read a randomly generated non-existent file.
88
88
@@ -109,7 +109,7 @@ def read_file(file):
109
109
read_file(f"{uuid.uuid4()}.txt")
110
110
```
111
111
112
-
The `pathlib.Path.unlink()` function has an optional parameter `missing_ok` that will suppress the `FileNotFoundError on file deletion`, if the parameter's value is `True` . However, without proper handling, using this parameter will cause the script to fail silently.
112
+
The `pathlib.Path.unlink()` function has an optional parameter `missing_ok` that will suppress the `FileNotFoundError` on file deletion, if the parameter's value is `True` . However, without proper handling, using this parameter will cause the script to fail silently.
0 commit comments