-
Notifications
You must be signed in to change notification settings - Fork 3
/
gSSURGO_Zipper.py
92 lines (65 loc) · 3.01 KB
/
gSSURGO_Zipper.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
# ---------------------------------------------------------------------------
# SSURGO_Zipper.py
# Created on: May 10, 2013
# Author: Charles.Ferguson
# Soil Scientist
# National Soil Survey Center
# USDA - NRCS
# e-mail: adolfo.diaz@usda.gov
# phone: 608.662.4422 ext. 216
# Author: Adolfo.Diaz
# GIS Specialist
# National Soil Survey Center
# USDA - NRCS
# e-mail: adolfo.diaz@usda.gov
# phone: 608.662.4422 ext. 216
#-------------------------------------------------------------------------------
# ================================================================================================================
def errorMsg():
try:
exc_type, exc_value, exc_traceback = sys.exc_info()
theMsg = "\t" + traceback.format_exception(exc_type, exc_value, exc_traceback)[1] + "\n\t" + traceback.format_exception(exc_type, exc_value, exc_traceback)[-1]
print(theMsg)
except:
pass
## ===================================================================================
# Import system modules
import sys, os, traceback, zipfile, glob
if __name__ == '__main__':
try:
# Directory containing exports
#rootDir = arcpy.GetParameterAsText(0)
#rootDir = r'E:\SSURGO'
#outDirectory = r'E:\Temp\zipTest'
rootDir = input("Enter Root Directory containing gSSURGO Datasets to Zip: ")
outDirectory = input("Enter Root Directory where gSSURGO Datasets will be Zipped: ")
gSSURGOList = glob.glob(f"{rootDir}\gSSURGO_*.gdb")
successfulZips = 0
print(f"\nThere are {len(gSSURGOList)} gSSURGO Datasets that will be zipped.")
# Iterate through every SSA path and zip up
for gSSURGO in gSSURGOList:
print(f"\nCreating zip archive for {os.path.basename(gSSURGO)}")
try:
zipName = os.path.basename(gSSURGO).split('.')[0] + '.zip'
zipFilePath = outDirectory + os.sep + zipName
if os.path.exists(zipFilePath):
print(f"\t{zipName} Exists. Deleting file")
os.remove(zipFilePath)
with zipfile.ZipFile(zipFilePath, 'w', zipfile.ZIP_DEFLATED) as outZip:
print(f"\tCreating: {zipName}")
for dirpath, dirnames, filenames in os.walk(gSSURGO):
for filename in filenames:
outZip.write(os.path.join(dirpath, filename), os.path.basename(gSSURGO) + os.sep + filename)
successfulZips+=1
print(f"\tSuccessfully Archived: {zipName}")
outZip.close()
except:
print("Problems zipping up " + gSSURGO)
errorMsg()
continue
if successfulZips > 0:
print("\tSuccessfully zipped " + str(successfulZips) + " SSURGO export datasets")
peaceOut = input("\nDone: Hit Enter to quit")
except:
errorMsg()
input("\n\nError: Hit Enter to quit")