-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-gz-to-csv.py
More file actions
35 lines (29 loc) · 1.15 KB
/
Copy pathextract-gz-to-csv.py
File metadata and controls
35 lines (29 loc) · 1.15 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
"""
extract-gz-to-csvs.py - Extract all csv files from .gzip into the related category folder.
Author: Wilson França de Souza
Email: wilson.franca.92@gmail.com
Date: 7/28/2020
"""
# Import modules
import gzip
import glob
import os.path
from basefunctions import extractCategoryfromCSVName, makedirIfNotExists
# Directory Paths
source_dir = r"D:\Documents\Rice University\Work\Highways + Watersheds\StormEvents"
stormEventsCsv_dir = os.path.join(source_dir, "StormEvents CSV")
makedirIfNotExists(stormEventsCsv_dir)
dest_dir = os.path.join(stormEventsCsv_dir, "N00_Original")
makedirIfNotExists(dest_dir)
# Search for files with *.gz extension in a folder
for src_name in glob.glob(os.path.join(source_dir, "*.gz")):
base = os.path.basename(src_name)
category = extractCategoryfromCSVName(base, "_", "-")
category_folder_path = os.path.join(dest_dir, category)
makedirIfNotExists(category_folder_path)
dest_name = os.path.join(category_folder_path, base[:-3])
# Extract and export the csv
with gzip.open(src_name, "rb") as infile:
with open(dest_name, "wb") as outfile:
for line in infile:
outfile.write(line)