forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBase.py
More file actions
62 lines (54 loc) · 1.78 KB
/
DataBase.py
File metadata and controls
62 lines (54 loc) · 1.78 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import shelve
import os.path
class DataBase(object):
def isEmpty(self, fileName):
file_object = open(fileName, "r")
for line in file_object:
if(line.strip() != ""):
file_object.close()
return False
file_object.close()
return True
def ifFile(self,fileName):
if not os.path.isfile(fileName):
#print "File does not exist"
return False
return True
def write(self, fileName, string):
file_object = open(fileName, "a")
file_object.write(string+"\n")
file_object.close()
def read(self, fileName):
if not self.ifFile(fileName):
return
file_object = open(fileName, "r")
for line in file_object:
print line,
file_object.close()
def getKey(self, fileName):
if not self.ifFile(fileName):
return
file_object = open(fileName, 'r')
lines = file_object.readlines()
file_object.close()
i = 0
while(lines[i].strip() == ""):
i = i+1
key = lines[i]
file_object = open(fileName, 'w')
file_object.write(''.join(lines[i+1:]))
file_object.close()
#if file empty then delete it
if(self.isEmpty(fileName)):
os.remove(fileName)
return key
def writeToHash(self, fileName, key, value):
hashFile_object = shelve.open(fileName)
hashFile_object[key] = value
hashFile_object.close()
def readFromHash(self, fileName, key):
hashFile_object = shelve.open(fileName)
if (key in hashFile_object):
value = hashFile_object[key]
hashFile_object.close()
return value