main.read_write¶
Functions¶
is_valid_filename ¶
Check if a filename is valid for common file systems.
Validates a filename against common file system restrictions including illegal characters, reserved patterns, and naming conventions. Cross-platform compatible for Windows, macOS, and Linux.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename | str | The filename to validate. | required |
Returns:
| Name | Type | Description |
|---|---|---|
bool | bool | True if valid, False otherwise. |
Examples:
>>> is_valid_filename("test.txt")
True
>>> is_valid_filename("test")
False
>>> is_valid_filename("file<name>.txt")
False
Source code in pyadvtools/main/read_write.py
read_list ¶
Read a text file and return its content as a list of lines.
Reads a text file and returns its content as a list of strings, with proper handling of file paths, existence checks, and content formatting. Uses Unix-style line endings (\n) consistently across all platforms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name | str | Name of the file to read. | required |
read_flag | str | File open mode (default: "r" for read). | 'r' |
path_storage | str | None | Optional directory path to prepend to file_name. | None |
Returns:
| Type | Description |
|---|---|
list[str] | List[str]: List of file lines with Unix-style line endings (\n), or empty list if file doesn't exist. |
Examples:
Note
This function enforces Unix-style line endings (\n) regardless of the host operating system. All line endings are normalized to \n during reading.
Source code in pyadvtools/main/read_write.py
write_list ¶
write_list(
data_list,
file_name,
write_flag="w",
path_storage=None,
check=True,
delete_first_empty=True,
delete_last_empty=True,
compulsory=False,
delete_original_file=False,
)
Write data to a file with comprehensive file handling.
Writes a list of strings or bytes to a file with extensive options for file handling, validation, and content processing. Enforces Unix-style line endings (\n) for text files across all platforms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_list | list[str] | list[bytes] | List of strings or bytes to write. | required |
file_name | str | Target file name. | required |
write_flag | str | File open mode ('w', 'a', 'wb', etc.). | 'w' |
path_storage | str | None | Optional directory path for the file. | None |
check | bool | If True, checks if file exists before overwriting. | True |
delete_first_empty | bool | Remove empty lines from start of data. | True |
delete_last_empty | bool | Remove empty lines from end of data. | True |
compulsory | bool | Write file even if data is empty. | False |
delete_original_file | bool | Delete existing file if data is empty. | False |
Returns:
| Name | Type | Description |
|---|---|---|
None | None | Writes to file or prints error messages. |
Examples:
>>> write_list(["line1", "line2"], "output.txt")
# Writes lines to output.txt with Unix-style line endings
Note
- Text files are always written with Unix-style line endings (\n) regardless of the host operating system
- Binary files are written as-is without line ending conversion
- Empty line removal occurs before line ending normalization
Source code in pyadvtools/main/read_write.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 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 | |