Files
A File
object is rarely used directly by end users, but it is a common base class for input and output data structures that may contain files. The File
object enables transparent serialization of binary file data for serialization to .json
, .yaml
, or .toml
which makes saving data structures to disk in a human-readable format or sending them over HTTP seamless even when they contain binary data.
qcio.Files
¶
File model for handling string and binary data.
Binary data is encoded as base64 strings during serialization.
Attributes:
Name | Type | Description |
---|---|---|
files |
Dict[str, Union[str, bytes]]
|
A dict mapping filename to str or bytes data. |
add_file
¶
add_file(
filepath: Union[Path, str],
relative_dir: Optional[Path] = None,
) -> None
Add a file to the object. The file will be added at to the files
attribute
with the filename as the key and the file data as the value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Union[Path, str]
|
The path to the file. |
required |
relative_dir |
Optional[Path]
|
The directory to make the file relative to. Helpful when adding files from a subdirectory. |
None
|
Example
my_obj.add_file("path/to/file.txt")
print(my_obj.files)
# Output: {"file.txt": "file data"}
Source code in qcio/models/base_models.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
add_files
¶
add_files(
directory: StrOrPath,
recursive: bool = False,
exclude: Optional[List[str]] = None,
) -> None
Add all files in a directory to the object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory |
StrOrPath
|
The directory to add files from. |
required |
recursive |
bool
|
Whether to recursively add files from subdirectories. |
False
|
exclude |
Optional[List[str]]
|
A list of filenames to exclude from the directory. |
None
|
Source code in qcio/models/base_models.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
|
save_files
¶
save_files(directory: StrOrPath = Path('.')) -> None
Write all files to the specified directory
Source code in qcio/models/base_models.py
269 270 271 272 273 274 275 276 277 278 |
|