diff --git a/src/kernel_services/plugin_entry_points/kernel.ml b/src/kernel_services/plugin_entry_points/kernel.ml
index 69334701d761700e73e48909a577b265821a44f3..9b4ae61a64246bc9d7aefa81625d64de917c09b3 100644
--- a/src/kernel_services/plugin_entry_points/kernel.ml
+++ b/src/kernel_services/plugin_entry_points/kernel.ml
@@ -719,10 +719,12 @@ module PrintReturn =
 module CodeOutput = struct
 
   let () = Parameter_customize.set_group inout_source
-  include P.Empty_string
+  include P.Filepath
       (struct
         let option_name = "-ocode"
         let arg_name = "filename"
+        let existence = Filepath.Indifferent
+        let file_kind = "source"
         let help =
           "when printing code, redirects the output to file <filename>"
       end)
@@ -731,22 +733,23 @@ module CodeOutput = struct
 
   let output job =
     let file = get () in
-    if file = ""
+    if Filepath.Normalized.(is_special_stdout file || is_empty file)
     then Log.print_delayed job
     else
       try
         let fmt =
           try fst (Hashtbl.find streams file)
           with Not_found ->
-            let out = open_out file in
+            let out = open_out (file:>string) in
             let fmt = Format.formatter_of_out_channel out in
             Hashtbl.add streams file (fmt,out) ; fmt
         in
         job fmt
       with Sys_error s ->
         warning
-          "Fail to open file \"%s\" for code output@\nSystem error: %s.@\n\
-           Code is output on stdout instead." file s ;
+          "Fail to open file \"%a\" for code output@\nSystem error: %s.@\n\
+           Code is output on stdout instead."
+          Filepath.Normalized.pretty file s ;
         Log.print_delayed job
 
   let close_all () =
@@ -757,8 +760,8 @@ module CodeOutput = struct
            close_out cout ;
          with Sys_error s ->
            failure
-             "Fail to close output file \"%s\"@\nSystem error: %s."
-             file s)
+             "Fail to close output file \"%a\"@\nSystem error: %s."
+             Filepath.Normalized.pretty file s)
       streams
 
   let () = Extlib.safe_at_exit close_all
diff --git a/src/kernel_services/plugin_entry_points/kernel.mli b/src/kernel_services/plugin_entry_points/kernel.mli
index fda42e772bc6373129f2859251baaac758f61837..8c74bc0b8673d5e28856e00545395b1a00151c48 100644
--- a/src/kernel_services/plugin_entry_points/kernel.mli
+++ b/src/kernel_services/plugin_entry_points/kernel.mli
@@ -328,7 +328,7 @@ module PrintReturn : Parameter_sig.Bool
 (** Behavior of option "-ocode".
     @plugin development guide *)
 module CodeOutput : sig
-  include Parameter_sig.String
+  include Parameter_sig.Filepath
   val output: (Format.formatter -> unit) -> unit
 end