-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_to_work_with_files.py
More file actions
47 lines (41 loc) · 1.62 KB
/
Copy pathclass_to_work_with_files.py
File metadata and controls
47 lines (41 loc) · 1.62 KB
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
'''
Данный класс позволяет удобно работать с файлами. Реализованы такие функции:
-Сложение файлов(создастся новый объект, содержание которого равно содержанием двух предыдущих
-Итерация по строкам файла, а не по словам
-При печати объекта выводится его путь
'''
import tempfile
import os
class File:
def __init__(self, filename):
self.filename = filename
self.counter = 0
def write(self,text):
with open(self.filename, 'w') as f:
f.write(text)
return None
def __iter__(self):
return self
def __next__(self):
f = open(self.filename,'r')
if(self.counter < len(f.readlines())):
f.seek(0)
for i in range(self.counter):
f.readline()
self.counter += 1
text = f.readline().strip("\n")
return text
else:
raise StopIteration
def __str__(self):
return self.filename
def __add__(self, another_object):
storage_path = os.path.join(tempfile.gettempdir(), 'storage.data')
with open(self.filename, 'r') as f1:
first_file_data = f1.read()
with open(another_object.filename, 'r') as f2:
second_file_data = f2.read()
with open(storage_path, 'w') as temp_file:
temp_file.write(first_file_data)
temp_file.write(second_file_data)
return File(storage_path)