-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextFileManager.py
executable file
·67 lines (59 loc) · 1.6 KB
/
TextFileManager.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
57
58
59
60
61
62
63
64
65
66
67
#! /usr/bin/env python
import os.path
class TextFileManager:
def __init__(self, fileName):
self.fileName=fileName
if (os.path.isfile(fileName)):
pass
#print "we have file:", fileName
else:
pass
#print "we have no file:", fileName
#self.fileHandle=open(fileName, 'w+')
#self.fileHandle.close()
def writeLines(self,stringList):
self.fileHandle=open(self.fileName, 'w+')
for lineItem in stringList:
self.fileHandle.write(lineItem+ os.linesep)
self.fileHandle.close()
def readLines(self):
result=list()
self.fileHandle=open(self.fileName, 'r')
lineList=self.fileHandle.readlines();
self.fileHandle.close()
for line in lineList:
line=line.rstrip('\n')
result.append(line)
return result
def writeString(self,inputString):
self.fileHandle=open(self.fileName, 'w+')
self.fileHandle.write(inputString)
self.fileHandle.close()
def readString(self):
result=""
self.fileHandle=open(self.fileName, 'r')
lineList=self.fileHandle.readlines();
self.fileHandle.close()
for line in lineList:
result+=line
return result
def insertLineAfter(self, insertedLine, preLine):
bSearched=False
newLineList=[]
lineList=self.readLines()
for line in lineList:
if bSearched:
newLineList.append(insertedLine)
bSearched=False
if preLine==line:
bSearched=True
newLineList.append(line)
self.writeLines(newLineList)
def insertLineBefore(self, insertedLine, suffixLine):
newLineList=[]
lineList=self.readLines()
for line in lineList:
if suffixLine==line:
newLineList.append(insertedLine)
newLineList.append(line)
self.writeLines(newLineList)