要从子文件夹中检索文件名,可以使用多种编程语言中的文件系统操作功能。以下是几种常见编程语言的示例代码:
import os
def list_files_in_subfolders(directory):
file_names = []
for root, dirs, files in os.walk(directory):
for file in files:
file_names.append(os.path.join(root, file))
return file_names
# 使用示例
directory_path = '/path/to/your/directory'
files = list_files_in_subfolders(directory_path)
for file in files:
print(file)
const fs = require('fs');
const path = require('path');
function listFilesInSubfolders(dir) {
let fileNames = [];
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
fileNames = fileNames.concat(listFilesInSubfolders(filePath));
} else {
fileNames.push(filePath);
}
}
return fileNames;
}
// 使用示例
const directoryPath = '/path/to/your/directory';
const files = listFilesInSubfolders(directoryPath);
files.forEach(file => console.log(file));
import java.io.File;
public class ListFiles {
public static void main(String[] args) {
File directory = new File("/path/to/your/directory");
listFiles(directory);
}
public static void listFiles(File directory) {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
listFiles(file);
} else {
System.out.println(file.getAbsolutePath());
}
}
}
}
}
通过上述示例代码和应用场景,您可以根据自己的需求选择合适的编程语言和方法来实现从子文件夹中检索文件名的功能。
领取专属 10元无门槛券
手把手带您无忧上云