python搜索指定目录的方法

yipeiwu_com6年前Python基础

本文实例讲述了python搜索指定目录的方法。分享给大家供大家参考。具体分析如下:

#-------------------------------------
#      Name: search_directory.py
#     Author: Kevin Harris
# Last Modified: 02/13/04
# Description: This Python script demonstrates how to use os.walk()
#         to walk through a directory hierarchy
#         and list everything 
#         found.
#-------------------------------------
import os
for root, dirs, files in os.walk( os.curdir ):
 print( "root = " + root )
 for file in files:
 print( "file = " + file )
 for dir in dirs:
 print( "dir = " + dir )
 print( "\n" )
input( '\n\nPress Enter to exit...' )

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python运行报错UnicodeDecodeError的解决方法

Python2.7在Windows上有一个bug,运行报错: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in...

Python pyinotify模块实现对文档的实时监控功能方法

0x01 安装pyinotify >>> pip install pyinotify >>> import pyinotify 0x02 实现对...

python实现从一组颜色中找出与给定颜色最接近颜色的方法

本文实例讲述了python实现从一组颜色中找出与给定颜色最接近颜色的方法。分享给大家供大家参考。具体分析如下: 这段代码非常有用,可以找到指定颜色相似的颜色,比如有一组8个颜色,现在给定...

通过代码实例展示Python中列表生成式的用法

1 平方列表 如果你想创建一个包含1到10的平方的列表,你可以这样做: squares = [] for x in range(10): squares.append(x**2)...

pytorch实现对输入超过三通道的数据进行训练

案例背景:视频识别 假设每次输入是8s的灰度视频,视频帧率为25fps,则视频由200帧图像序列构成.每帧是一副单通道的灰度图像,通过pythonb里面的np.stack(深度拼接)可将...