diff --git a/binarycpython/utils/HPC.py b/binarycpython/utils/HPC.py
index 7b543fb5c8be4c0be6e7c162bc24ecfcc805ddb0..33343107ac6bacf5ed4ac330ab52414b7085da3d 100644
--- a/binarycpython/utils/HPC.py
+++ b/binarycpython/utils/HPC.py
@@ -9,6 +9,7 @@
     Condor API.
 """
 import datetime
+import flufl.lock
 import glob
 import os
 import pathlib
@@ -79,17 +80,33 @@ class HPC(condor,slurm):
                 self.exit(code=1)
             x = False
         else:
-            # write the joiningfile
-            print("Making joiningfile at {file} with range 0 to {n}".format(
-                file=file,
-                n=n
-            ))
-            with open(file,"w",encoding="utf-8") as f:
-                for i in range(0,n):
-                    f.write(os.path.join(prefix,
-                                         "{i}.gz\n".format(i=i)))
-                    f.close()
-            x = True
+            # lock the joiningfile
+            lock = flufl.lock.Lock(file)
+
+            # writing this file should not take > 1 hour
+            lock.lifetime = datetime.timedelta(seconds=3600)
+
+            if os.path.isfile(file):
+                print("Cannot make joiningfile at {file} because it already exists.".format(file=file))
+                x = False
+            else:
+            if lock.is_locked:
+                # write the joiningfile
+                print("Making joiningfile at {file} with range 0 to {n}".format(
+                    file=file,
+                    n=n
+                ))
+                with open(file,"w",encoding="utf-8") as f:
+                    for i in range(0,n):
+                        f.write(os.path.join(prefix,
+                                             "{i}.gz\n".format(i=i)))
+                    x = True
+                else:
+                    print("Could not lock joiningfile at {file}".format(file=file))
+                    x = False
+                    
+            # unlock the file
+            lock.unlock()
         return x
                     
     def HPC_joinfiles(self,joinlist=None):