Skip to content

Commit 54b52d0

Browse files
authored
Merge pull request #2 from EMSS-Antennas/error-handle
Code refactoring and directory re-enforcing
2 parents d0e25d6 + 20fc643 commit 54b52d0

2 files changed

Lines changed: 65 additions & 41 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.spec
44
.zip
55
.pyc
6+
.txt

dataExtract.py

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,78 @@
11
import os, sys
22
import pandas as pd
33

4-
def extractExcelData(serialNumber, outputFile):
5-
# Specify the root directory where you want to search for folders
6-
rootDir = 'C:/Alphawave Services/EA Production - SARAO - SARAO/DocumentControl/Test data/317-022005' # Replace with your actual directory path
7-
4+
def checkFolder(serialNumber, Dir):
85
# Search for the folder with the specified serial number
9-
for folderName in os.listdir(rootDir):
10-
folderPath = os.path.join(rootDir, folderName)
6+
for folderName in os.listdir(Dir):
7+
folderPath = os.path.join(Dir, folderName)
118
if os.path.isdir(folderPath) and folderName == serialNumber:
12-
# Look for an Excel file (.xlsx) within the serialNumber folder
13-
for fileName in os.listdir(folderPath):
14-
if fileName.lower().endswith('.xlsx'):
15-
excelFilePath = os.path.join(folderPath, fileName)
16-
break
17-
else:
18-
print(f"No Excel file found in folder '{serialNumber}'.")
19-
return
9+
return folderPath
10+
else:
11+
pass
12+
return "False"
2013

21-
# Read the Excel file and extract relevant data
22-
try:
23-
df = pd.read_excel(excelFilePath, sheet_name="Introduction", header=None)
24-
serial_row = df.iloc[10, 8] # Columns I to N (0-based index)
25-
# print(serial_row)
26-
if serial_row == serialNumber:
27-
performance_df = pd.read_excel(excelFilePath, sheet_name="Performance")
28-
dataToWrite = performance_df.iloc[2:11, 3].tolist() # D4 to D17
29-
dataToWrite.append(performance_df.iloc[15, 3])
30-
else:
31-
print(f"Serial number '{serialNumber}' not found in the Excel file.")
32-
return
33-
except Exception as e:
34-
print(f"Error reading Excel file: {e}")
35-
return
14+
def checkExcel(FP, SN):
15+
# Look for an Excel file (.xlsx) within the serialNumber folder
16+
for fileName in os.listdir(FP):
17+
if fileName.lower().endswith('.xlsx'):
18+
# excelFilepath
19+
return os.path.join(FP, fileName)
20+
else:
21+
pass
22+
logError("error.txt", f"No Excel file found in folder '{SN}'.")
23+
return "False"
3624

37-
# Write the extracted data to a text file
38-
outputFile = rootDir + "/" + serialNumber + "/" + outputFile
39-
with open(outputFile, 'w') as txtFile:
40-
for line, cellValue in enumerate(dataToWrite, start=1):
41-
txtFile.write(f"{cellValue}\n")
42-
print(f"Data written to '{outputFile}' successfully.")
25+
def extractExcelData(serialNumber, outputFile):
26+
# Specify the root directory where you want to search for folders
27+
symbolicRootDir = '/Alphawave Services/EA Production - SARAO - SARAO/DocumentControl/Test data/317-022005' # Replace with your actual directory path
28+
rootDir = os.getcwd()
29+
30+
folderPath = checkFolder(serialNumber, rootDir)
31+
32+
if folderPath == "False":
33+
folderPath = checkFolder(serialNumber, symbolicRootDir)
34+
if folderPath == "False":
35+
logError("error.txt", f"Directory not found.")
36+
return
37+
# Read the Excel file and extract relevant data
38+
try:
39+
excelFilePath = checkExcel(folderPath, serialNumber)
40+
if excelFilePath == "False":
4341
return
42+
43+
df = pd.read_excel(excelFilePath, sheet_name="Introduction", header=None)
44+
serial_row = df.iloc[10, 8] # Columns I to N (0-based index)
45+
if serial_row == serialNumber:
46+
performance_df = pd.read_excel(excelFilePath, sheet_name="Performance")
47+
dataToWrite = performance_df.iloc[2:11, 3].tolist() # D4 to D17
48+
dataToWrite.append(performance_df.iloc[15, 3])
49+
else:
50+
logError("error.txt", f"Serial number '{serialNumber}' not found in the Excel file.")
51+
return
52+
except Exception as e:
53+
logError("error.txt", f"Error reading Excel file: {e}")
54+
return
55+
56+
# Write the extracted data to a text file
57+
outputFile = folderPath + "/" + serialNumber + ".txt"
58+
with open(outputFile, 'w') as txtFile:
59+
for line, cellValue in enumerate(dataToWrite, start=1):
60+
txtFile.write(f"{cellValue}\n")
4461

45-
print(f"Folder '{serialNumber}' not found in the specified directory.")
62+
def logError(file, errMsg):
63+
with open(file, 'a') as textFile:
64+
textFile.write(errMsg)
4665

4766
if __name__ == "__main__":
4867

4968
if len(sys.argv) != 2:
50-
print("Usage: python dataExtract.py <Serial Number>")
69+
err = f"Please add serial number as an argument.\n"
70+
logError("error.txt", err)
5171
sys.exit(1)
52-
serialNumberArg = sys.argv[1]
53-
outputFile = serialNumberArg + ".txt"
54-
55-
extractExcelData(serialNumberArg, outputFile)
72+
else:
73+
serialNumberArg = sys.argv[1]
74+
outputFile = serialNumberArg + ".txt"
75+
try:
76+
extractExcelData(serialNumberArg, outputFile)
77+
except Exception:
78+
logError("error.txt", f"Application failed to run.\n")

0 commit comments

Comments
 (0)