{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f1de3e2e-cc89-4b12-a2fb-42c1270755ee", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import scipy as sc\n", "\n", "import os\n", "import glob\n", "from pathlib import Path\n", "import warnings" ] }, { "cell_type": "code", "execution_count": 2, "id": "38262129-8cd6-447a-b0b5-554ab370f153", "metadata": {}, "outputs": [], "source": [ "def list_files_pathlib(\n", " directory_path, \n", " verbose=None,\n", "):\n", " \"\"\"\n", " Lists files using the modern pathlib module and returns them as a list.\n", " \n", " Args:\n", " directory_path (str): The path to the directory to inspect.\n", " verbose (bool): If True, prints detailed output to the console. \n", " Defaults to False.\n", "\n", " Returns:\n", " list: A list of file names (str) found in the directory. \n", " Returns an empty list if the directory is not found or is empty.\n", " \"\"\"\n", "\n", " import os\n", " import glob\n", " from pathlib import Path\n", " import warnings\n", " \n", " if verbose is None:\n", " verbose = False\n", " elif verbose is True:\n", " print(f\"--- Listing contents in '{directory_path}' using pathlib ---\")\n", " \n", " try:\n", "\n", " # Step 1 - Check if the directory_path is valid\n", " # Note - this code is almost too bulletproof? I love it :D\n", " path_obj = Path(directory_path)\n", " if not path_obj.exists():\n", " warnings.warn(f\"Directory not found at '{directory_path}'\", UserWarning)\n", " return [] # Return empty list on error\n", " if not path_obj.is_dir():\n", " warnings.warn(f\"The path '{directory_path}' is not a directory.\", UserWarning)\n", " return [] # Return empty list on error\n", " entries = sorted(list(path_obj.iterdir()))\n", " if not entries:\n", " warnings.warn(f\"The directory at '{directory_path}' is empty.\", UserWarning)\n", " return [] # Return empty list on error\n", " \n", "\n", " # Step 2 - Collect all the files in the directory\n", " files_list = []\n", " entries = sorted(list(path_obj.iterdir()))\n", " for entry in entries:\n", " if entry.is_file():\n", " files_list.append(str(entry))\n", " if verbose:\n", " entry_type = \"dir\" if entry.is_dir() else \"file\"\n", " print(f\"- {entry.name:<20} ({entry_type})\") \n", " return files_list\n", "\n", " except Exception as e:\n", " warnings.warn(f\"An unexpected error occurred: {e}\", RuntimeWarning)\n", " return [] # Return empty list on unexpected exception\n", " finally:\n", " if verbose:\n", " print(\"-\" * 50)\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "76cae071-2cee-48e5-9465-e7c392ed135d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['HIPed_Stellite1/EDS_Point_Carbide1.txt',\n", " 'HIPed_Stellite1/EDS_Point_Carbide2.txt',\n", " 'HIPed_Stellite1/EDS_Point_Carbide3.txt',\n", " 'HIPed_Stellite1/EDS_Point_SolidSolution1.txt',\n", " 'HIPed_Stellite1/EDS_Point_SolidSolution2.txt',\n", " 'HIPed_Stellite1/EDS_Point_SolidSolution3.txt']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filenames = list_files_pathlib(\"HIPed_Stellite1\")\n", "filenames" ] }, { "cell_type": "code", "execution_count": 4, "id": "b9825c82-42b0-47b2-afca-81466478f841", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | cps/eV | \n", "
---|---|
keV | \n", "\n", " |
-0.20 | \n", "0.0 | \n", "
-0.19 | \n", "0.0 | \n", "
-0.18 | \n", "0.0 | \n", "
-0.17 | \n", "0.0 | \n", "
-0.16 | \n", "0.0 | \n", "
... | \n", "... | \n", "
20.23 | \n", "0.0 | \n", "
20.24 | \n", "0.0 | \n", "
20.25 | \n", "0.0 | \n", "
20.26 | \n", "0.0 | \n", "
20.27 | \n", "0.0 | \n", "
2048 rows × 1 columns
\n", "