-
Notifications
You must be signed in to change notification settings - Fork 52
/
utils.py
42 lines (33 loc) · 996 Bytes
/
utils.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
import sys, os
def get_files_with_extension(directory, extension):
files = []
for name in os.listdir(directory):
if name.endswith(extension):
files.append(f'{directory}/{name}')
return files
def read_file(file):
with open(file, 'r') as f:
data = f.readlines()
data = [line.replace("Â", " ").strip().split() for line in data]
return data
def write_to_file(file, string, append=False):
if append:
mode = 'a'
else:
mode = 'w'
with open(file, mode) as f:
f.write(string)
def write_data_to_file(file, data, separator, append=False):
if append:
mode = 'a'
else:
mode = 'w'
with open(file, mode) as f:
for item in data:
f.write(f'{item}{separator}')
def input_to_output(input_file, output_directory):
return (
os.path.join(output_directory, os.path.basename(input_file))
.replace("input", "output")
.replace(".in", ".out")
)