-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindfiles.py
More file actions
64 lines (56 loc) · 1.62 KB
/
Copy pathfindfiles.py
File metadata and controls
64 lines (56 loc) · 1.62 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
63
64
import os
import numpy as np
def whichfile(extension='.bands', directory = '~/' ):
"""
Parameters
----------
extension :
(Default value = '.bands')
Here is the extention of the file that you want to extract form the directory.
directory :
(Default value = '~/')
The Directory.
Returns
-------
It returns the name of the full path of the file.
"""
list_files = os.listdir(directory)
for i in list_files:
if extension in i :
myfile = directory+i
break
return myfile
def extract_pattern(f=str, pattern = 'Total =',indexofvalue=-1,name ='no_name'):
"""
Parameters
----------
f :
(Default value = str)
Path of the file.
pattern :
(Default value = 'Total =')
Pattern to extract from the file
indexofvalue :
(Default value = -1)
Example: If the pattern line contains 3 numbers by default it will takes the last one (-1) but if you want the second one add set 'indexofvalue=1'or
'indexofvalue=-2'
Returns
-------
returns a tuple of the (pattern, value)
"""
with open(f,'r') as input:
lines = input.readlines()
ee = []
for i in lines:
if pattern in i:
try:
ee.append(float(i.split()[indexofvalue]))
if len(float(i.split()[indexofvalue])) == 0:
ee.append(np.nan)
except:
ee.append(np.nan)
if name=='no_name':
name=pattern
else :
name=name
return (name,ee[0])