-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_Python_utilities.py
197 lines (168 loc) · 6.54 KB
/
_Python_utilities.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
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
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 22 01:00:19 2018
@author: [email protected]
"""
#%%
from datetime import datetime, timedelta
import time
import os
import shutil
from pathlib import Path
import numpy as np
#from astropy.io import fits
#%%
# =============================================================================
# mkdir
# =============================================================================
def mkdir(fpath, mode=0o777, exist_ok=True):
''' Convenience function for Path.mkdir()
'''
fpath = Path(fpath)
Path.mkdir(fpath, mode=mode, exist_ok=exist_ok)
#%%
# =============================================================================
# creat log.format(fullname, , )=======================================================
def write_log(log_file, log_str):
timestamp = datetime.now()
timestamp = timestamp.strftime(r'%Y-%m-%d %H:%M:%S')
msg = '[' + timestamp + '] ' + log_str
print(msg)
with open(log_file, 'a') as f:
f.write(msg + '\n')
def write_log_old(log_file, log_str):
timestamp = time.strftime(r'%Y-%m-%d %H:%M:%S')
msg = '[' + timestamp + '] ' + log_str
print(msg)
with open(log_file, 'a') as f:
f.write(msg + '\n')
def write_log2(log_file, log_str):
with open(log_file, 'a') as log_f:
log_f.write("{}, {}\n".format(os.path.basename(__file__), log_str))
return print ("{}, {}\n".format(os.path.basename(__file__), log_str))
#%%
# =============================================================================
# for checking time
# =============================================================================
cht_start_time = datetime.now()
def print_working_time(cht_start_time):
working_time = (datetime.now() - cht_start_time) #total days for downloading
return print('working time ::: %s' % (working_time))
#%%
# =============================================================================
#
# =============================================================================
def nearest_date(items, pivot) :
nearest = min(items,
key=lambda x: abs(x - pivot))
timedelta = abs(nearest - pivot)
return nearest, timedelta
def nearest_ind(items, pivot) :
time_diff = np.abs([date - pivot for date in items])
return time_diff.argmin(0)
#%%
# =============================================================================
#
# =============================================================================
def removeAllEmptyDirs(fpath):
"""
Parameters
----------
fullname : fpath
The fullname of input directory...
"""
del_N = 0
for i in range(10) :
fullnames = getFullnameListOfallsubDirs(fpath)
#print ("fullnames: {}".format(fullnames))
print ("{} directories were found... ".format(len(fullnames)))
for fullname in fullnames[:] :
# Check is empty..
print('Check directory {} is empty or not...'.format(fullname))
if len(os.listdir(fullname)) == 0 :
# Delete..
shutil.rmtree(r"{}".format(fullname))
del_N =+1
print ("rmtree {}\n".format(fullname))
return print("Total {} directories deleted...".format(del_N))
#%%
# =============================================================================
# getFullnameListOfallFiles
# =============================================================================
def getFullnameListOfallFiles(dirName):
##############################################3
import os
# create a list of file and sub directories
# names in the given directory
listOfFile = sorted(os.listdir(dirName))
allFiles = list()
# Iterate over all the entries
for entry in listOfFile:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getFullnameListOfallFiles(fullPath)
else:
allFiles.append(fullPath)
return allFiles
# =============================================================================
# getFullnameListOfallFiles
# =============================================================================
def getFullnameListOfallsubDirs1(dirName):
##############################################3
import os
allFiles = list()
for file in sorted(os.listdir(dirName)):
d = os.path.join(dirName, file)
allFiles.append(d)
if os.path.isdir(d):
allFiles.extend(getFullnameListOfallsubDirs1(d))
return allFiles
# =============================================================================
# getFullnameListOfallsubDirs
# =============================================================================
def getFpaths(dirName):
##############################################3
import os
allFiles = list()
for it in os.scandir(dirName):
if it.is_dir():
allFiles.append(it.path)
allFiles.extend(getFullnameListOfallsubDirs(it))
allFiles = [w+"/" for w in allFiles]
return allFiles
# =============================================================================
# getFullnameListOfallsubDirs
# =============================================================================
def getFullnameListOfallsubDirs(dirName):
##############################################3
import os
allFiles = list()
for it in os.scandir(dirName):
if it.is_dir():
allFiles.append(it.path)
allFiles.extend(getFullnameListOfallsubDirs(it))
allFiles = [w+"/" for w in allFiles]
return allFiles
# =============================================================================
# getFullnameListOfsubDir
# =============================================================================
def getFullnameListOfsubDirs(dirName):
##############################################3
import os
allFiles = list()
for it in os.scandir(dirName):
if it.is_dir():
allFiles.append(it.path)
allFiles = [w+"/" for w in allFiles]
return allFiles
# %%
def getListOfFullnameInDir(dirName):
##############################################3
import os
allFiles = list()
for it in os.scandir(dirName):
allFiles.append(it.path)
#allFiles = [w+"/" for w in allFiles]
return allFiles