File System Module
anonipy.utils.file_system
The module containing the file_system
utilities.
The file_system
module provides a set of utilities for reading and writing files.
Functions:
Name | Description |
---|---|
open_file |
Opens a file and returns its content as a string. |
write_file |
Writes the text to a file. |
open_json |
Opens a JSON file and returns its content as a dictionary. |
write_json |
Writes the data to a JSON file. |
open_file(file_path)
Opens a file and returns its content as a string.
Examples:
>>> from anonipy.utils import file_system
>>> file_system.open_file("path/to/file.txt")
"Hello, World!"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
The path to the file. |
required |
Returns:
Type | Description |
---|---|
str
|
The content of the file as a string. |
Source code in anonipy/utils/file_system.py
write_file(text, file_path, encode=True)
Writes the text to a file.
Examples:
>>> from anonipy.utils import file_system
>>> file_system.write_file("Hello, World!", "path/to/file.txt")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to write to the file. |
required |
file_path
|
str
|
The path to the file. |
required |
encode
|
Union[str, bool]
|
The encoding to use. |
True
|
Raises:
Type | Description |
---|---|
TypeError
|
If text, |
FileNotFoundError
|
If the directory does not exist. |
Source code in anonipy/utils/file_system.py
open_json(file_path)
Opens a JSON file and returns its content as a dictionary.
Examples:
>>> from anonipy.utils import file_system
>>> file_system.open_json("path/to/file.json")
{"hello": "world"}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
The path to the JSON file. |
required |
Returns:
Type | Description |
---|---|
dict
|
The content of the JSON file as a dictionary. |
Source code in anonipy/utils/file_system.py
write_json(data, file_path)
Writes data to a JSON file.
Examples:
>>> from anonipy.utils import file_system
>>> file_system.write_json({"hello": "world"}, "path/to/file.json")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict
|
The data to write to the JSON file. |
required |
file_path
|
str
|
The path to the JSON file. |
required |