Skip to content
Advertisement

How do I check if a file is an executable in nim?

How do I check if a file is an executable on Linux using Nim? Thanks in advance.

Advertisement

Answer

You can use getFilePermissions and check if a certain FilePermission is in the set it returns.

import os

let isExecutable = fpOthersExec in getFilePermissions "./filename"

You’d probably want to check if all three different Exec variants are in it:

import os

proc isExecutable(filename: string): bool =
  let filePermissions = getFilePermissions filename
  fpUserExec in filePermissions and
    fpGroupExec in filePermissions and
      fpOthersExec in filePermissions
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement