write data to a specified file, if it exists, ask to overwrite
(data_to_write, filename, mode=None)
| 240 | |
| 241 | |
| 242 | def write_to_file(data_to_write, filename, mode=None): |
| 243 | """ |
| 244 | write data to a specified file, if it exists, ask to overwrite |
| 245 | """ |
| 246 | global stop_animation |
| 247 | |
| 248 | if os.path.exists(filename): |
| 249 | if not mode: |
| 250 | stop_animation = True |
| 251 | is_append = lib.output.prompt("would you like to (a)ppend or (o)verwrite the file") |
| 252 | if is_append.lower() == "o": |
| 253 | mode = "w" |
| 254 | elif is_append.lower() == "a": |
| 255 | mode = "a+" |
| 256 | else: |
| 257 | lib.output.error("invalid input provided ('{}'), appending to file".format(is_append)) |
| 258 | lib.output.error("Search results NOT SAVED!") |
| 259 | |
| 260 | if mode == "w": |
| 261 | lib.output.warning("Overwriting to {}".format(filename)) |
| 262 | if mode == "a": |
| 263 | lib.output.info("Appending to {}".format(filename)) |
| 264 | |
| 265 | else: |
| 266 | # File does not exists, mode does not matter |
| 267 | mode = "w" |
| 268 | |
| 269 | with open(filename, mode) as log: |
| 270 | if isinstance(data_to_write, (tuple, set, list)): |
| 271 | for item in list(data_to_write): |
| 272 | log.write("{}{}".format(item.strip(), os.linesep)) |
| 273 | else: |
| 274 | log.write(data_to_write) |
| 275 | lib.output.info("successfully wrote info to '{}'".format(filename)) |
| 276 | return filename |
| 277 | |
| 278 | |
| 279 | def load_api_keys(unattended=False, path="{}/etc/tokens".format(CUR_DIR)): |