使用python列出目录下的所有文件
2024-08-28 02:41:54
https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python
You can use glob
:
import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
print(file)
or simply os.listdir
:
import os
for file in os.listdir("/mydir"):
if file.endswith(".txt"):
print(os.path.join("/mydir", file))
or if you want to traverse directory, use os.walk
:
import os
for root, dirs, files in os.walk("/mydir"):
for file in files:
if file.endswith(".txt"):
print(os.path.join(root, file))
最新文章
- [ASP.NET Core] Getting Started
- hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)
- python递归理解图
- 使用nssm在windows服务器上部署nodejs
- [游戏模版6] Win32 graph
- sql字段类型介绍
- 【题解】【BST】【Leetcode】Convert Sorted Array to Binary Search Tree
- linux挂载文件
- 土地购买 usaco 斜率优化
- UIPickerView基本用法
- Boxes in a Line
- voucer
- navigaitonBar的自定义设置
- Extjs 4.0 Tab页
- cocos creator主程入门教程(三)—— 资源管理
- Python中面向对象的概念(科普)
- Matlab - 基础知识
- tensorFlow入门实践(一)
- WPF备忘录(4)打个勾画个叉娱乐下
- Asp.net 用户控件和自定义控件注册