根据deno documentation for writeFile,如果我要写入文件,如果文件已经存在,则覆盖我需要此命令-
await Deno.writeFile("hello1.txt", data); // overwrite "hello1.txt" or create it
我需要创建一个空文件,如果已经存在同名文件,则用一个空文件覆盖它,因此我尝试在没有数据的情况下运行该函数,并且出现此错误。
await Deno.writeFile("hello1.txt");
An argument for 'data' was not provided.
data: Uint8Array,
如何在Deno中创建一个空文件并覆盖存在相同名称的文件?
答案 0 :(得分:3)
将空的import regex
pattern = r"(?:xpath\(//|\G(?!^))[^@]*@[^=]*='(\d+)'"
s = "xpath(//Edge[@Id='3' or @Id='74'])"
print(regex.findall(pattern, s));
传递到['3', '74']
Uint8Array
您还可以将Deno.writeFile
与await Deno.writeFile("./hello1.txt", new Uint8Array());
一起使用
Deno.open
truncate: true
或者,如果您知道文件已经存在,则可以使用Deno.truncate
await Deno.open("./hello1.txt", { create: true, write: true, truncate: true });
答案 1 :(得分:2)
您可以使用Deno.create
:
如果不存在则创建文件,或者将其截断并解析为
Deno.File
的实例。
await Deno.create("hello1.txt")
安全处理可能不存在的目录的更深文件路径:
import { ensureFile } from "https://deno.land/std/fs/ensure_file.ts";
const file = "./deep/hello1.txt"; // `deep` doesn't need to exist
await ensureFile(file); // ensures file and containing directories
await Deno.truncate(file);
答案 2 :(得分:1)
您可以使用writeTextFile
并传递一个空的string
。按照上述建议休息,将ensureFile and truncate
用于其他需求。
(async function() {
await Deno.writeTextFile("./helloworld.txt", "")
})()