diff --git a/pulsar/client/test/check.py b/pulsar/client/test/check.py
index 9273b6425696ba9f54052e18505e9394611c2f12..25c69cd91c0908d5094deb3833717a5b56b1cba0 100644
--- a/pulsar/client/test/check.py
+++ b/pulsar/client/test/check.py
@@ -417,8 +417,10 @@ def __write_to_file(path, contents):
     dirname = os.path.dirname(path)
     if not os.path.exists(dirname):
         os.makedirs(dirname)
-    with open(path, "wb") as file:
-        file.write(contents)
+    with open(path, "wb") as f:
+        if not isinstance(contents, binary_type):
+            contents = binary_type(contents, "UTF-8")
+        f.write(contents)
 
 
 def __makedirs(directories):