Last active
May 11, 2022 04:01
-
-
Save jet-c-21/e4151f70a3655a41b8fc62e7101b182d to your computer and use it in GitHub Desktop.
Common Python File I/O Tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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