使用Python,我创建了一个请求输入(操作)的脚本。第二个选项在新的终端窗口中运行名为settings.js
的NodeJS文件,其中包含:
subprocess.call('start node src/settings.js', shell=True)`
在NodeJSsettings.js
文件中,它基本上使用fs
覆盖config.json
中的键值。在这种情况下,将键"PREFIX"
更改为用户输入的任何内容(例如!
)
当它第一次读取config.json
文件时,它显示一个错误
代码:
await fs.readFile('config.json', 'utf8', async (err, data) => {
// code to overwrite (with given data)
}
错误:
[Error: ENOENT: no such file or directory, open 'C:\Users\userhere\Desktop\serenity-zero\serenity-zero-test\config.json'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\userhere\\Desktop\\serenity-zero\\serenity-zero-test\\config.json'
}
我估计这是因为它最初是从python
脚本运行的,所以目录文件夹在NodeJS文件(该python
脚本的文件夹目录)之外。我试过:
await fs.readFile('src/config.json', 'utf8', async (err, data) => {
// code
});
以及
await fs.readFile('./src/config.json', 'utf8', async (err, data) => {
// code
});
但是,我得到了:
(node:17348) UnhandledPromiseRejectionWarning: Error: Cannot find module './src/config.json'
Require stack:
- C:\Users\userhere\Desktop\serenity-zero\serenity-zero-test\src\settings.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at C:\Users\userhere\Desktop\serenity-zero\serenity-zero-test\src\settings.js:29:87
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:17348) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:17348) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
正如您所知,当python
脚本与node.js
文件位于同一文件夹中时,这种方法工作得非常好。我想让它更有条理,所以我把node.js
文件放在src
文件夹中。python
脚本仍保留在原始文件夹中。在那之后,我犯了这个错误
刚刚修复了它,我所要做的就是将
config.json
放在与launch.py
文件相同的目录中。我认为config.json
不可能在src
文件夹中,但至少它可以工作相关问题 更多 >
编程相关推荐