Newer
Older
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import glob
import os
import shutil
def filePathCorr(filePath):
if(filePath[-1]=='\\'):
return filePath
else:
return filePath+'\\'
def fileExtensionSingle(fileNameList):
if(len(fileNameList)<4):
fileNameList=fileNameList+".txt"
else:
if(fileNameList!=".txt"):
fileNameList=fileNameList+".txt"
return fileNameList
def fileExtension(fileNameList):
for i in range(len(fileNameList)):
int_file=fileNameList[i]
if(len(int_file)<4):
fileNameList[i]=fileNameList[i]+".txt"
else:
if(int_file[-4:]!=".txt"):
fileNameList[i]=fileNameList[i]+".txt"
return fileNameList
def fileExtensionRemover(fileNameList):
for i in range(len(fileNameList)):
int_file=fileNameList[i]
if(int_file[-4:]==".txt"):
fileNameList[i]=fileNameList[i].replace(".txt","")
# fileNameList[i]=fileNameList[i].replace("_"," ")
return fileNameList
def inFolder(file_name): #takes an array
file_list=glob.glob("*.txt")
count=0
for i in range(len(file_name)):
flag=0
while(flag==0):
for fp in enumerate(file_list):
if(file_name[i]==fp[1]):
print(file_name[i]," found in dir")
count=count+1
flag=1
if(flag==0):
print(file_name[i]," not in dir")
flag=1
if(count==len(file_name)):
print("all files found in dir. Ok to continue...")
return 1
else:
print("missing files in dir.Resuming...")
return 0
def descarFileConfig(fp,sp,descar_path):
with open(descar_path+"\\boot.txt","r") as file:
fpi,spi=file.readlines()[0].split(',')
if(fp!="null" and sp!="null"):
fpi=fp
spi=sp
if(fp!="null" and sp=="null"):
fpi=fp
elif(fp=="null" and sp!="null"):
spi=sp
elif(fp=="null" and sp=="null"):
print("Path configuration unchanged")
else:
print("\n!!CRITICAL ERROR!! Unable to access, load or write the DesCar path confifg file: boot.txt\nUsing DesCar past this error might result in data overwritte\n")
with open(descar_path+"\\boot.txt","w") as file:
file.write(fpi+","+spi)
def complete_file_path(fp,descar_path):
if(fp!=""):
os.chdir(filePathCorr(fp))
descarFileConfig(fp,"null",descar_path)
print("\nFile path changed to:",filePathCorr(fp))
return fp
else:
print("\nThe current file path is:\n", os.getcwd())
while(1):
a=input(' Do you want to keep it ? (y/n): ')
if(a=='n'):
fpi=filePathCorr(input("Please provide a file path to the data source folder: "))
os.chdir(fpi)
descarFileConfig(fp,"null",descar_path)
print("The path has been changed to: ", fpi)
break
elif(a=='y'):
fpi=filePathCorr(os.getcwd())
print("Current file directory kept")
break
else:
print("Command not recognised. Resuming...")
return fpi
def complete_save_path(sp,csp,descar_path):
if(sp!=""):
save_dir=filePathCorr(sp)
descarFileConfig("null",sp,descar_path)
print("\nSave path changed to: ",filePathCorr(save_dir))
return save_dir
else:
print("\nThe current save path is:\n", csp)
while(1):
a=input(' Do you want to keep it ? (y/n): ')
if(a=='n'):
spi=filePathCorr(input("Please provide a save path: "))
descarFileConfig("null",sp,descar_path)
print("The path has been changed to: ", spi)
break
elif(a=='y'):
spi=filePathCorr(csp)
print("Current save directory kept")
break
else:
print("Command not recognised. Resuming...")
return spi
def file_length(file):
with open(file) as fp:
x=[]
for line in fp:
values = line.split()
x.append(float(values[0]))
return len(x)
def legend(file_list):
while(1):
a=input("Do you want to display the automatic legend ? (y/n): ")
if(a=="y"):
print("The legend will be:",fileExtensionRemover(file_list))
legend=fileExtensionRemover(file_list)
break
elif(a=="n"):
legend=input("What legend do you want to give to the curve sir ? (S1,S2,): ")
legend=legend.split(',')
print("The legend will be:",legend)
break
else:
print("Command not recognised. Resuming")
return legend
def file_duplication():
files=glob.glob("*.txt")
print("Found files:",files)
newpath=os.getcwd()+"\modified_files"+"\\"
print("The current path:",os.getcwd())
if not os.path.exists(newpath):
os.makedirs(newpath)
for i, fp in enumerate(files):
shutil.copy2(os.getcwd()+"\\"+files[i],newpath)
print("All filed copied to: ",newpath)
os.chdir(newpath)
files=glob.glob("*.txt")
for i, fp in enumerate(files):
os.rename(files[i],"no_Si_peaks_"+files[i])
return(newpath)
class Extension:
def __init__(self):
self.old=None
self.new=None
def set_old(self,old):
self.old=old
def get_old(self):
return self.old
def set_new(self,new):
self.new=new
def get_new(self):
return self.new
def clear_extension(self):
self.old=None
self.new=None
extensionset=Extension()
def old_ext(old_ext):
if(old_ext==''):
a=input("\nPlease provide the old extension (.txt, .csv, ...) with the dot: ")
extensionset.set_old(a)
print("Old extenstion set to: ",a,"\n")
else:
extensionset.set_old(old_ext)
print("Old extenstion set to: ",old_ext,"\n")
def new_ext(new_ext):
if(new_ext==''):
a=input("\nPlease provide the new extension (.txt, .csv, ...) with the dot: ")
extensionset.set_new(a)
print("New extenstion set to: ",a,"\n")
else:
extensionset.set_new(new_ext)
print("New extenstion set to: ",new_ext,"\n")
def extClear():
extensionset.clear_extension()
print("\nGeneral extension settings cleared succesfully !\n")
def extension():
try:
if(extensionset.get_old()==None):
old=input("\nWhat extension do you want to replace (.csv, .txt, ...): ")
else:
old=extensionset.get_old()
files=glob.glob("*"+old)
f_list=fileExtensionRemover(glob.glob("*"+old))
print("\n")
for i in range(len(files)):
print("Found: ",files[i])
if(extensionset.get_new()==None):
new=input("\nWhat extension do you want to use (.csv, .txt, ...): ")
else:
new=extensionset.get_new()
print("\n")
for i,fp in enumerate(files):
with open(fp) as file:
values=[]
for line in file:
values.append(line)
with open (f_list[i].replace(old,new),'w') as new_file:
print("Outputing ",f_list[i].replace(old,new))
for k in range(len(values)):
new_file.write(values[k])
print('\n---------------------------------------------------------------\n')
print("\nOperation successful!\n")
except:
print('\n---------------------------------------------------------------\n')
print("\nError in the mail loop. Exiting ...\n")
def trimFN(trim):
try:
if(trim==""):
trim=input("\nSequence to remove from the file name: ")
files=glob.glob("*.txt")
f_list=fileExtensionRemover(glob.glob("*.txt"))
for i,fp in enumerate(files):
if(trim in fp):
print("Found: ",fp)
print("\n")
print("\nRemoving characters ...\n")
for i,fp in enumerate(files):
if(trim in fp):
with open(fp) as file:
values=[]
for line in file:
values.append(line)
with open (f_list[i].replace(trim,"")+".txt",'w') as new_file:
print("Outputing: ",f_list[i].replace(trim,""))
for k in range(len(values)):
new_file.write(values[k])
print('\n---------------------------------------------------------------\n')
print("\nOperation successful!\n")
except:
print('\n---------------------------------------------------------------\n')
print("\nError in the mail loop. Exiting ...\n")