Skip to content

Instantly share code, notes, and snippets.

@jet-c-21
Last active May 11, 2022 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jet-c-21/e4151f70a3655a41b8fc62e7101b182d to your computer and use it in GitHub Desktop.
Save jet-c-21/e4151f70a3655a41b8fc62e7101b182d to your computer and use it in GitHub Desktop.
Common Python File I/O Tool
import os
import shutil
def create_dir(fp: str):
if not os.path.exists(fp):
os.mkdir(fp)
def remove_dir(fp: str):
if os.path.exists(fp):
shutil.rmtree(fp)
import json
def read_json(fp: str):
return json.load(open(fp, 'r', encoding='utf-8'))
def to_json(data, fp: str):
json.dump(data, open(fp, 'w', encoding='utf-8'), indent=4, ensure_ascii=False)
import pickle
def read_pickle(fp: str):
with open(fp, 'rb') as f:
return pickle.load(f)
def to_pickle(data, fp: str):
with open(fp, 'wb') as f:
pickle.dump(data, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment