-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.py
52 lines (42 loc) · 1.37 KB
/
basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -----------------------------------
# BASIC HELPFUL FUNCTIONS
# -----------------------------------
import os
import pandas as pd
# -----------------------------------
# READING/WRITING FILES
# -----------------------------------
# READ_FILE()
# -----------------------------------
# FILEPATH = str(); name of the file/filepath
# TEXT/DF = file content
# read files differently depending on their extension
def read_file(filepath):
if filepath[-4:] == ".pkl":
df = pd.read_pickle(filepath)
return df
elif filepath[-4:] == ".txt":
with open(filepath, "r", encoding="utf-8") as textfile:
text = textfile.read()
return text
# -----------------------------------
# CREATING DIRECTORIES
# -----------------------------------
# GET_CURRENT_DIR()
# -----------------------------------
# NO INPUT
# CURRENT_DIR = str(); actual working path
def get_current_dir():
current_dir = os.getcwd()
return current_dir
# CREATE_DIR()
# -----------------------------------
# DIRNAME = str(); name of the directory to create
def create_dir(dirName):
current_dir = get_current_dir()
dir_to_create = current_dir + "\\" + dirName
try:
os.makedirs(dir_to_create)
print("Directory " , dir_to_create , " created.")
except FileExistsError:
print("Directory " , dir_to_create , " already exists, save data there.")