In short
- for write: use open(“filepath”, “w”)
- for read: use open(“filepath”, “r”)
- for closing files: use “with” for auto closing the files (realpython)
Python
# write/read text
with open("/tmp/my_file.txt", "w") as file:
file.write("Eat more kale")
with open("/tmp/my_file.txt", "r") as file:
content = file.read()
# write/read json
import json
with open("tmp/my_file.json", "w") as file:
json.dump({"vegatble": "kale", "healthy": True }, file)
with open("tmp/my_file.json", "r") as file:
data = json.load(file)
How would you optimize saving expensive API calls?
Python
def save_read_expensive_api():
content = expensive_api()
dir_path = f"tmp/files"
file_name = f"sample.txt"
file_path = os.path.join(dir_path, file_name)
os.makedirs(dir_path, exist_ok=True)
with open(file_path, "w") as file:
print("saving file of length: ", len(content))
file.write(content)
# with open(file_path, "r") as file:
# print("reading file of length: ", len(content))
# content = file.read()
return content
Explanation
Say you have a function with output you want to save. It could be from:
- long running calculations (I’ve been running the nurse scheduling problem)
- expensive API calls (LLM providers that charge per token 💵)
Python
content = expensive_call(...)
Step: Create the directory you are saving to. I usually write to the system tmp directory or a project tmp tmp directory and put “tmp” in my gitignore so I don’t accidentally commit.
Python
dir_path = f"/tmp/files"
file_name = f"sample.txt"
# this creates the directories if they dont exist
os.makedirs(dir_path, exist_ok=True)
file_path = os.path.join(dir_path, file_name)
Step: make the call and save the output, commenting out the read
Python
# this writes to the file
content = expensive_call(...)
with open(file_path, "w") as file:
file.write(content)
# this reads from the file
# with open(file_path, "r") as file:
# content = file.read()
Step: comment out the call and read the file!
Python
# this writes to the file
# content = expensive_call(...)
#
# with open(file_path, "w") as file:
# file.write(content)
# this reads from the file
with open(file_path, "r") as file:
content = file.read()