diff --git a/bin/hicstuff_build_matrix.py b/bin/hicstuff_build_matrix.py
index 194a9d395964a37a5349b7fd7052d5fb0a93af12..cf3fea288b882f13a0166c5b9d44b5e4a3b94214 100755
--- a/bin/hicstuff_build_matrix.py
+++ b/bin/hicstuff_build_matrix.py
@@ -72,7 +72,8 @@ def pairs2matrix(
     # )
 
     cmd = 'grep -v ^# %s | sort -S 2G -k8,8n -k9,9n --parallel=1' % pairs_file
-    os.system(cmd '>>' prem_mat_file)
+    with open(pre_mat_file, "a") as fout:
+        fout.write(os.system(cmd))
 
     header_size = len(hio.get_pairs_header(pre_mat_file))
     with open(pre_mat_file, "r") as pairs, open(mat_file, "w") as mat: