【问题标题】:How can one check if a file or directory exists using Deno?如何使用 Deno 检查文件或目录是否存在?
【发布时间】:2019-11-01 15:06:29
【问题描述】:

Deno TypeScript 运行时有 built-in functions,但它们都没有解决检查文件或目录是否存在的问题。如何检查文件或目录是否存在?

【问题讨论】:

    标签: typescript deno


    【解决方案1】:

    有标准库实现,这里:https://deno.land/std/fs/mod.ts

    import {existsSync} from "https://deno.land/std/fs/mod.ts";
    
    const pathFound = existsSync(filePath)
    console.log(pathFound)
    

    如果路径存在,此代码将打印true,否则将打印false

    这是异步实现:

    import {exists} from "https://deno.land/std/fs/mod.ts"
    
    exists(filePath).then((result : boolean) => console.log(result))
    

    确保使用不稳定标志运行 deno 并授予对该文件的访问权限:

    deno run --unstable  --allow-read={filePath} index.ts
    

    【讨论】:

      【解决方案2】:

      自 Deno 1.0.0 发布以来,Deno API 发生了变化。如果找不到该文件,则引发的异常是 Deno.errors.NotFound

      const exists = async (filename: string): Promise<boolean> => {
        try {
          await Deno.stat(filename);
          // successful, file or directory must exist
          return true;
        } catch (error) {
          if (error instanceof Deno.errors.NotFound) {
            // file or directory does not exist
            return false;
          } else {
            // unexpected error, maybe permissions, pass it along
            throw error;
          }
        }
      };
      
      exists("test.ts").then(result =>
        console.log("does it exist?", result)); // true
      
      exists("not-exist").then(result =>
        console.log("does it exist?", result)); // false
      
      

      由于原来的答案账号被暂停,如果我评论它不能改变他的答案,我正在重新发布一个固定的代码sn-p。

      【讨论】:

        【解决方案3】:

        exists 函数实际上是 std/fs 模块的一部分,尽管它目前被标记为不稳定。这意味着你需要deno run --unstablehttps://deno.land/std/fs/README.md#exists

        【讨论】:

          【解决方案4】:

          没有专门用于检查文件或目录是否存在的函数,但the Deno.stat function 可以通过检查Deno.ErrorKind.NotFound 的潜在错误来用于此目的,它返回有关路径的元数据。

          const exists = async (filename: string): Promise<boolean> => {
            try {
              await Deno.stat(filename);
              // successful, file or directory must exist
              return true;
            } catch (error) {
              if (error && error.kind === Deno.ErrorKind.NotFound) {
                // file or directory does not exist
                return false;
              } else {
                // unexpected error, maybe permissions, pass it along
                throw error;
              }
            }
          };
          
          exists("test.ts").then(result =>
            console.log("does it exist?", result)); // true
          
          exists("not-exist").then(result =>
            console.log("does it exist?", result)); // false
          

          【讨论】:

            猜你喜欢
            • 2012-05-17
            • 1970-01-01
            • 2013-04-24
            • 2018-05-13
            • 1970-01-01
            • 2013-04-07
            • 2011-04-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多