Python list all files in a folder and its subdirectories
python简介
Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。
Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。
像Perl语言一样, Python 源代码同样遵循 GPL(GNU General Public License)协议。
>>> import os >>> for i in os.walk("."): ... print i[0],"\n##",i[1],"\n##",i[2] ... . #当前目录 ## ['fa', 'out'] #当前目录中的子目录 ## ['meta_rna.sh', 'nohup.out', 'log.cpu', 'blast_seq.py'] ./fa # 第一个子目录 ## [] # 第一个子目录中的目录 ## ['assemblyar_new_2.faa'] ./out # 第二个子目录 ## [] # 第二个子目录中的目录 ## ['assemblyar_new_2.faa.coord', 'assemblyar_new_2.faa.mask', 'assemblyar_new_2.faa.seq', 'result_1.xm', 'result.xml', 'blast_seq.py']
也可以用 os.path.walk, 先定义一个访问文件夹的函数, VisitDir
>>> def VisitDir(arg, dirname, names): ... for filespath in names: ... print os.path.join(dirname, filespath) ... >>> path="." >>> os.path.walk(path, VisitDir, ()) ./meta_rna.sh ./fa ./out ./nohup.out ./log.cpu ./blast_seq.py ./fa/assemblyar_new_2.faa ./out/assemblyar_new_2.faa.coord ./out/assemblyar_new_2.faa.mask ./out/assemblyar_new_2.faa.seq ./out/result_1.xm ./out/result.xml ./out/blast_seq.py >>> os.getcwd() '/home/served_pro/Find_nick' >>> abs_path= os.getcwd() >>> os.path.walk(abs_path, VisitDir, ()) /home/served_pro/Find_nick/meta_rna.sh /home/served_pro/Find_nick/fa /home/served_pro/Find_nick/out /home/served_pro/Find_nick/nohup.out /home/served_pro/Find_nick/log.cpu /home/served_pro/Find_nick/blast_seq.py /home/served_pro/Find_nick/fa/assemblyar_new_2.faa /home/served_pro/Find_nick/out/assemblyar_new_2.faa.coord /home/served_pro/Find_nick/out/assemblyar_new_2.faa.mask /home/served_pro/Find_nick/out/assemblyar_new_2.faa.seq /home/served_pro/Find_nick/out/result_1.xm /home/served_pro/Find_nick/out/result.xml /home/served_pro/Find_nick/out/blast_seq.py
下面给大家介绍python列出文件夹下的所有文件
#方法1:使用os.listdir import os for filename in os.listdir(r'c:\\windows'): print filename #方法2:使用glob模块,可以设置文件过滤 import glob for filename in glob.glob(r'c:\\windows\\*.exe'): print filename #方法3:通过os.path.walk递归遍历,可以访问子文件夹 import os.path def processDirectory ( args, dirname, filenames ): print 'Directory',dirname for filename in filenames: print ' File',filename os.path.walk(r'c:\\windows', processDirectory, None ) #方法4:非递归 import os for dirpath, dirnames, filenames in os.walk('c:\\\\winnt'): print 'Directory', dirpath for filename in filenames: print ' File', filename
另外,判断文件与目录是否存在:
import os os.path.isfile('test.txt') #如果不存在就返回False os.path.exists(directory) #如果目录不存在就返回False
以上所述是小编给大家介绍的Python列出一个文件夹及其子目录的所有文件,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHP中文网的支持!
更多Python列出一个文件夹及其子目录的所有文件相关文章请关注PHP中文网!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
