-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_file.py
56 lines (44 loc) · 1.69 KB
/
read_file.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
53
54
55
56
import os
import glob
import json
"""Reading in text files."""
def read_file(filename):
"""Read a plain text file and return the contents as a string."""
with open(filename, "r") as f:
text = f.read()
return text
def read_files(path):
"""Read all files that match given path and return a dict with their contents."""
# TODO: Get a list of all files that match path (hint: use glob)
files_in_path = []
for filename in glob.iglob(os.path.join(os.getcwd() + '/' + path)):
files_in_path.append(filename)
print("files_in_path: ", files_in_path)
# TODO: Read each file using read_file()
# TODO: Store & return a dict of the form { <filename>: <contents> }
# Note: <filename> is just the filename (e.g. "hieroglyph.txt") not the full path ("data/hieroglyph.txt")
file_contents_mapping = {}
for path in enumerate(files_in_path):
path = path[1]
extracted_filename_from_path = path.split("/")[-1]
file_contents_mapping[extracted_filename_from_path] = read_file(path)
# print(json.dumps(file_contents_mapping, indent=2))
return file_contents_mapping
# ALTERNATIVE BY OTHERS
# files = glob.glob(path)
# my_dict = dict()
# for file in files:
# content = read_file(file)
# filename = os.path.basename(file)
# my_dict[filename] = content
# return my_dict
def test_run():
# Test read_file()
print("Hieroglyph file contents: ", read_file("data/hieroglyph.txt"))
# Test read_files()
texts = read_files("data/*.txt")
for name in texts:
print("\n***", name, "***")
print(texts[name])
if __name__ == '__main__':
test_run()