-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpypref.py
104 lines (81 loc) · 2.69 KB
/
pypref.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
# -*- coding: utf8 -*-
import os
from qgis.core import QgsProject
def readPref(txt0, arg0='pref.txt'):
"""Visszaad egy megadott parméterhez tartozó értéket
az arg0-ként megadott paraméter fájlból"""
#print(txt0, arg0)
if os.path.exists(arg0):
with open(arg0) as f:
for line in f:
linevals = line.split("=")
if linevals[0] == txt0:
#print("%s= %s" % (linevals[0], linevals[1].rstrip('\r\n')))
return linevals[1].rstrip('\r\n')
return 'None1'
else:
return 'None2'
def readAll(arg0='pref.txt'):
"""Visszaadja az arg0 paraméter fájl teljes tartalmát"""
if os.path.exists(arg0):
lines = ''
with open(arg0) as f:
for line in f:
lines += line
return lines
else:
return None
def writePref(txt0, txt1, arg0='pref.txt'):
"""egy megadott paraméterhez tartozó új értéket ad az arg0 paraméter fájlban"""
if os.path.exists(arg0):
with open(arg0) as f:
lines = [x for x in f.readlines() if x != "\r\n"]
fw = open(arg0, 'w')
for line in lines:
linevals = line.split("=")
if txt0 == linevals[0]:
fw.write(txt0 + '=' + txt1 + "\n")
print("%s= %s" % (txt0, linevals[1]))
elif line != "\r\n":
fw.write(line)
fw.close()
return None
else:
print('path error')
return None
def newParam(txt0, txt1, arg0='pref.txt'):
"""Új paraméter és a hozzá tartozó érték hozzáadása az arg0 paraméter fájlhoz"""
if os.path.exists(arg0):
with open(arg0) as f:
for x in f.readlines():
if x != "\r\n":
if txt0 == x.split("=")[0]:
return False
fw = open(arg0, 'a')
fw.write('\r\n' + txt0 + '=' + txt1)
fw.close()
return True
else:
print('path error')
return None
def delParam(txt0, arg0='pref.txt'):
"""egy megadott paramétert és a hozzá tartozó értéket törli az arg0 paraméter fájlból"""
if os.path.exists(arg0):
with open(arg0) as f:
lines = [x for x in f.readlines() if x != "\r\n"]
fw = open(arg0, 'w')
for line in lines:
linevals = line.split("=")
if txt0 != linevals[0]:
fw.write(line)
fw.close()
return None
else:
return None
if __name__ == '__main__':
pass
print(readPref('img'))
print(writePref('pkw', 'posz'))
print(newParam('pkw', 'posz'))
print(readAll())