-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-process.py
More file actions
72 lines (56 loc) · 1.82 KB
/
Copy pathpre-process.py
File metadata and controls
72 lines (56 loc) · 1.82 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
65
66
67
68
69
70
71
72
import argparse
import os
import zipfile
import shutil
# -----------------------------
# Argument parsing
# -----------------------------
parser = argparse.ArgumentParser()
parser.add_argument("--data_path", type=str, required=True)
args = parser.parse_args()
DATA_PATH = args.data_path
ZIP_NAME = "sentiment140"
TARGET_NAME = "sentiment_data_file.csv"
print("Data path:", DATA_PATH)
if not os.path.isdir(DATA_PATH):
raise FileNotFoundError(f"{DATA_PATH} is not a directory")
zip_path = os.path.join(DATA_PATH, ZIP_NAME)
if not os.path.isfile(zip_path):
raise FileNotFoundError(f"{ZIP_NAME} not found in {DATA_PATH}")
# -----------------------------
# Unzip
# -----------------------------
print("Unzipping:", zip_path)
with zipfile.ZipFile(zip_path, "r") as zip_ref:
print("Zip contents:")
zip_ref.printdir()
zip_ref.extractall(DATA_PATH)
# -----------------------------
# Find extracted CSV
# -----------------------------
extracted_csv = None
for root, _, files in os.walk(DATA_PATH):
for f in files:
if f.endswith(".csv") and f != TARGET_NAME:
extracted_csv = os.path.join(root, f)
break
if not extracted_csv:
raise RuntimeError("No CSV file found after unzip")
print("Extracted CSV found:", extracted_csv)
# -----------------------------
# Rename (or copy if rename fails)
# -----------------------------
target_path = os.path.join(DATA_PATH, TARGET_NAME)
try:
os.rename(extracted_csv, target_path)
print("Renamed to:", target_path)
except PermissionError:
print("Rename not allowed, copying instead...")
shutil.copyfile(extracted_csv, target_path)
print("Copied to:", target_path)
# -----------------------------
# Final verification
# -----------------------------
print("\nFinal files in dataset directory:")
for f in os.listdir(DATA_PATH):
print(" -", f)