-
Notifications
You must be signed in to change notification settings - Fork 1
/
Class_of_file.py
47 lines (37 loc) · 1.12 KB
/
Class_of_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
import os
import tempfile
class File:
def __init__(self, path):
self.path = path
def write(self, data):
with open(self.path, 'a') as f:
f.write(data)
def read(self):
with open(self.path, 'r') as f:
return f.read()
def __str__(self):
return self.path
def __add__(self, second):
dir_new = tempfile.gettempdir()
path_new = os.path.join(dir_new, 'new_file.txt')
newfile = File(path_new)
with open(newfile.path, 'a') as f:
f.write(self.read())
with open(newfile.path, 'a') as f:
f.write(second.read())
return newfile
def __iter__(self):
self.line = open(self.path, 'r')
return self.line
def next(self):
line = self.line.readline()
if line != '':
return line
raise StopIteration
# file1 = File('/home/nyarina/PycharmProjects/untitled3/file.txt')
# file2 = File('/home/nyarina/PycharmProjects/untitled3/file1.txt')
# new_file = file1 + file2
# new_file.write('arina')
# file1.write('arina')
# file1.write('arina1')
# print(file1.read())