Browse Source

Create clean_pgsql2shp_output.py

Thomas Kandler 6 years ago
parent
commit
92d05db131
1 changed files with 42 additions and 0 deletions
  1. 42
    0
      clean_pgsql2shp_output.py

+ 42
- 0
clean_pgsql2shp_output.py View File

@@ -0,0 +1,42 @@
1
+import os
2
+import sys
3
+# do this if package missing: pip install dbf 
4
+import dbf
5
+
6
+#TODO: add parameter for path to clean
7
+#TODO: add parameter for additional extensions
8
+
9
+# get dir of .py file a.k.a. current path
10
+filepath = os.path.dirname(os.path.abspath(__file__)) 
11
+
12
+# typical SHP file extension which need to be deleted
13
+extensions = ['dbf', 'prj', 'shp', 'shx']
14
+
15
+# counter for deleted files
16
+filesDeleted = 0
17
+
18
+for file in os.listdir(filepath):
19
+    if file.endswith(".dbf"):    
20
+        # open DBF file with dbf package; assumpation: any dbf file with 
21
+        # zero content is useless
22
+        checkDbf = dbf.Table(file)
23
+        checkDbf.open()
24
+        # check for empty DBF file
25
+        if len(checkDbf) == 0: 
26
+            # get file name without extension
27
+            delFile = file.split('.')[0]
28
+            # iterate with the possible extensions of the file name
29
+            for i in range(0, len(extensions)):
30
+                fileToRemove = ""
31
+                fileToRemove = delFile + '.' + extensions[i]
32
+                # need to close file before we can delete
33
+                checkDbf.close()
34
+                try:
35
+                    os.remove(fileToRemove)
36
+                    filesDeleted += 1
37
+                except:
38
+                    continue
39
+        else:
40
+            continue
41
+                
42
+print "\nDeleted {} files.".format(filesDeleted)