Repository: PlasmaPy/PlasmaPy

Issue:
Switch to using `pathlib` instead of `os.path`
In newer Python code, it's generally recommended to use [`pathlib`](https://docs.python.org/3/library/pathlib.html) instead of `os.path`.  There are [a few places where we use `os.path`](https://github.com/PlasmaPy/PlasmaPy/search?q=os.path+-plasmapy_sphinx) where we should switch to `pathlib`. 

There are also [places where we are using `pathlib`](https://github.com/PlasmaPy/PlasmaPy/search?q=pathlib&type=code) which could be used as examples.  

We shouldn't make this change in `docs/plasmapy_sphinx` since that's being moved to another package (#1643).

Thanks!


Discussion:
I can work on this issue :)! 

Resolving patch:
diff --git a/plasmapy/plasma/sources/openpmd_hdf5.py b/plasmapy/plasma/sources/openpmd_hdf5.py
--- a/plasmapy/plasma/sources/openpmd_hdf5.py
+++ b/plasmapy/plasma/sources/openpmd_hdf5.py
@@ -3,9 +3,9 @@
 import astropy.units as u
 import h5py
 import numpy as np
-import os
 
 from packaging.version import Version
+from pathlib import Path
 
 from plasmapy.plasma.exceptions import DataStandardError
 from plasmapy.plasma.plasma_base import GenericPlasma
@@ -55,7 +55,7 @@ class HDF5Reader(GenericPlasma):
     def __init__(self, hdf5, **kwargs):
         super().__init__(**kwargs)
 
-        if not os.path.isfile(hdf5):
+        if not Path(hdf5).is_file():
             raise FileNotFoundError(f"Could not find file: '{hdf5}'")
 
         h5 = h5py.File(hdf5, "r")
@@ -148,8 +148,7 @@ def is_datasource_for(cls, **kwargs):
         hdf5 = kwargs.get("hdf5")
         openPMD = kwargs.get("openPMD")
 
-        isfile = os.path.isfile(hdf5)
-        if not isfile:
+        if not Path(hdf5).is_file():
             raise FileNotFoundError(f"Could not find file: '{hdf5}'")
 
         if "openPMD" not in kwargs:


Test patch:
diff --git a/plasmapy/diagnostics/tests/test_charged_particle_radiography.py b/plasmapy/diagnostics/tests/test_charged_particle_radiography.py
--- a/plasmapy/diagnostics/tests/test_charged_particle_radiography.py
+++ b/plasmapy/diagnostics/tests/test_charged_particle_radiography.py
@@ -5,7 +5,6 @@
 import astropy.constants as const
 import astropy.units as u
 import numpy as np
-import os
 import pytest
 
 from scipy.special import erf
@@ -612,7 +611,7 @@ def test_saving_output(tmp_path):
     results_1 = sim.results_dict
 
     # Save result
-    path = os.path.join(tmp_path, "temp.npz")
+    path = str(tmp_path / "temp.npz")
     sim.save_results(path)
 
     # Load result
diff --git a/plasmapy/particles/data/test/__init__.py b/plasmapy/particles/data/test/__init__.py
--- a/plasmapy/particles/data/test/__init__.py
+++ b/plasmapy/particles/data/test/__init__.py
@@ -2,8 +2,8 @@
 Test`.h5` files came from
 https://github.com/openPMD/openPMD-example-datasets/tree/b4f87b817629b99a048026a2724a5b265810d8be
 """
-import glob
-import os
 
-data_dir = os.path.dirname(__file__)
-data_files = glob.glob(os.path.join(data_dir, "*.[!p]*"))
+from pathlib import Path
+
+data_dir = Path(__file__).parents[0]
+data_files = list(data_dir.glob("*.[!p]*"))
diff --git a/plasmapy/plasma/sources/tests/test_openpmd_hdf5.py b/plasmapy/plasma/sources/tests/test_openpmd_hdf5.py
--- a/plasmapy/plasma/sources/tests/test_openpmd_hdf5.py
+++ b/plasmapy/plasma/sources/tests/test_openpmd_hdf5.py
@@ -1,4 +1,3 @@
-import os
 import pytest
 
 from astropy import units as u
@@ -13,21 +12,21 @@
 
 @pytest.fixture(scope="module")
 def h5_2d(request):
-    h5 = openpmd_hdf5.HDF5Reader(hdf5=os.path.join(data_dir, "data00000255.h5"))
+    h5 = openpmd_hdf5.HDF5Reader(hdf5=data_dir / "data00000255.h5")
     yield h5
     h5.close()
 
 
 @pytest.fixture(scope="module")
 def h5_3d(request):
-    h5 = openpmd_hdf5.HDF5Reader(hdf5=os.path.join(data_dir, "data00000100.h5"))
+    h5 = openpmd_hdf5.HDF5Reader(hdf5=data_dir / "data00000100.h5")
     yield h5
     h5.close()
 
 
 @pytest.fixture(scope="module")
 def h5_theta(request):
-    h5 = openpmd_hdf5.HDF5Reader(hdf5=os.path.join(data_dir, "data00000200.h5"))
+    h5 = openpmd_hdf5.HDF5Reader(hdf5=data_dir / "data00000200.h5")
     yield h5
     h5.close()
 
@@ -147,7 +146,7 @@ def test_unavailable_hdf5():
 
 def test_non_openpmd_hdf5():
     with pytest.raises(DataStandardError):
-        openpmd_hdf5.HDF5Reader(hdf5=os.path.join(data_dir, "blank.h5"))
+        openpmd_hdf5.HDF5Reader(hdf5=data_dir.joinpath("blank.h5"))
 
 
 def test_HDF5Reader(h5_2d):
diff --git a/plasmapy/plasma/tests/test_plasma_factory.py b/plasmapy/plasma/tests/test_plasma_factory.py
--- a/plasmapy/plasma/tests/test_plasma_factory.py
+++ b/plasmapy/plasma/tests/test_plasma_factory.py
@@ -1,6 +1,5 @@
 import astropy.units as u
 import numpy as np
-import os
 import pytest
 
 import plasmapy.plasma
@@ -10,7 +9,7 @@
 
 @pytest.fixture(scope="module")
 def h5(request):
-    h5 = plasmapy.plasma.Plasma(hdf5=os.path.join(data_dir, "data00000255.h5"))
+    h5 = plasmapy.plasma.Plasma(hdf5=data_dir / "data00000255.h5")
     yield h5
     h5.close()