将案例扫描当前目录,得到所有文件名(只输出.c)
时间: 2024-05-01 21:21:37 浏览: 20
好的,你可以使用以下Python代码来实现:
```python
import os
# 获取当前目录
current_dir = os.getcwd()
# 扫描目录
for file in os.listdir(current_dir):
# 判断是否为.c文件
if file.endswith(".c"):
# 输出文件名
print(file)
```
这段代码会获取当前目录,然后扫描目录下的所有文件,如果文件名以".c"结尾,则输出文件名。你可以将这段代码保存为一个.py文件,然后在命令行中运行即可。
相关问题
C语言将案例扫描当前目录,得到所有文件名(只输出.c)
可以使用C语言中的dirent.h头文件中的函数来实现该功能。具体代码如下:
```c
#include <stdio.h>
#include <dirent.h>
#include <string.h>
int main(void) {
DIR *dir;
struct dirent *ent;
if ((dir = opendir(".")) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (strcmp(ent->d_name + strlen(ent->d_name) - 2, ".c") == 0) {
printf("%s\n", ent->d_name);
}
}
closedir(dir);
} else {
perror("");
return 1;
}
return 0;
}
```
该程序会打开当前目录,遍历其中的所有文件,如果文件名以".c"结尾,则将其输出。
shell脚本写一段当前路径下所有文件名,指定输出.apk后缀文件名代码
可以使用以下的 shell 脚本代码来实现:
```
#!/bin/bash
# 切换到当前目录
cd "$(dirname "$0")"
# 查找所有 .apk 后缀的文件并输出文件名
find . -type f -name "*.apk" -print
```
注意:这段代码只适用于 Linux 和 macOS 等类 Unix 系统,如果你在 Windows 上使用,需要使用类 Unix 终端工具,如 Git Bash 或 Cygwin 等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)