code-assistant / code_analysis.py
Mahinour1's picture
add basic files
a2383a1
raw
history blame contribute delete
941 Bytes
def read_python_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
return content
except FileNotFoundError:
print(f"File not found: {file_path}")
raise
except IOError as e:
print(f"Error reading file {file_path}: {str(e)}")
raise
except Exception as e:
print(f"Unexpected error reading file {file_path}: {str(e)}")
raise
def extract_imports(code, file_path):
try:
# Split into lines and find imports
import_lines = []
for line in code.split('\n'):
line = line.strip()
if line.startswith('import ') or line.startswith('from '):
import_lines.append(line)
return import_lines
except Exception as e:
print(f"Error extracting imports from file {file_path}: {str(e)}")
return []