@@ -17,22 +17,12 @@ import fs from 'fs';
1717import path from ' path' ;
1818
1919function listItems (directory ) {
20- fs .readdir (directory, (err , items ) => {
21- if (err) {
22- console .log (` 读取目录时出错:${ err} ` );
23- return ;
24- }
25-
26- items .forEach ((item ) => {
27- const itemPath = path .join (directory, item);
28- fs .stat (itemPath, (err , stats ) => {
29- if (err) {
30- console .log (` 获取${ item} 的信息时出错:${ err} ` );
31- } else {
32- console .log (` ${ item} :${ stats .isDirectory () ? ' 目录' : ' 文件' } ` );
33- }
34- });
35- });
20+ const items = fs .readdirSync (directory);
21+
22+ items .forEach ((item ) => {
23+ const itemPath = path .join (directory, item);
24+ const stats = fs .statSync (itemPath);
25+ console .log (` ${ item} :${ stats .isDirectory () ? ' 目录' : ' 文件' } ` );
3626 });
3727}
3828
@@ -61,8 +51,8 @@ list_items('../')
6151| 特性 | JavaScript | Python |
6252| ---------| ------------| --------|
6353| 文件或目录是否存在 | fs.existsSync(path) | os.path.exists(path) |
64- | 创建目录 | fs.mkdir (path, [ options ] , callback ) | os.mkdir(path) |
65- | 列出目录 | fs.readdir (path, [ options ] , callback ) | os.listdir(path) |
54+ | 创建目录 | fs.mkdirSync (path) | os.mkdir(path) |
55+ | 列出目录 | fs.readdirSync (path) | os.listdir(path) |
6656
6757### 相关资源
6858
0 commit comments