遍历目录的小程序

遍历目录,列出下面所有文件名(带目录名),并且把所有文件名存入listfile列表,最后显示列表的内容。

# -*- coding: cp936 -*-

import os,sys
def listdir(dir):
    fielnum = 0
    listfile = []
    list = os.listdir(dir)  #列出目录下的所有文件和目录
    for line in list:
        filepath = os.path.join(dir,line)
        if os.path.isdir(filepath):  #如果filepath是目录,则再列出该目录下的所有文件
            for li in os.listdir(filepath):
                print filepath + li
                listfile.append(filepath + li)
                fielnum = fielnum + 1
        elif os.path:   #如果filepath是文件,直接列出文件名
            fielnum = fielnum + 1
            print filepath + line
            listfile.append(filepath + line)
    print ('文件总数是'+ str(fielnum))
    print listfile
    
#dir = raw_input('please input the path:')
dir = "D:\\tools"
listdir(dir)







第02个小程序遍历画笔(FlipThroughTheBrushes.cs) using System; using System.Reflection; using System.Windows; using System.Windows.Input; using System.Windows.Media; namespace Chapter02 { public class FlipThroughTheBrushes : Window { int index = 0; //索引值 PropertyInfo[] props; //属性数组 [STAThread] public static void Main() { Application app = new Application(); app.Run(new FlipThroughTheBrushes()); } public FlipThroughTheBrushes() { //取得Brushes类的成员,放在props中 props = typeof(Brushes).GetProperties(BindingFlags.Public | BindingFlags.Static); SetTitleAndBackground(); } //鼠标按下时 protected override void OnKeyDown(KeyEventArgs args) { if (args.Key == Key.Down || args.Key == Key.Up) { //Key.Down时index-1,Key.UP时index+1 index += args.Key == Key.Up ? 1 : props.Length - 1; //index对属性数组长度取余,防止索引越界 index %= props.Length; SetTitleAndBackground(); } base.OnKeyDown(args); } //设置背景 void SetTitleAndBackground() { Title = "Flip Through the Brushes - " + props[index].Name; Background = (Brush)props[index].GetValue(null, null); //设置背景画笔 } } } GetProperties()函数用于返回对象数组,参数是限制Brushes公开和静态的属性。其实这里可以不需要这样的限制,因为Brushes的属性本来全部都是public和static。 props[index].Name返回第index个属性的名称;props[index].GetValue(null, null)返回实际的SolidColorBrush对象,第一个参数是属性所在的对象,因为Brushes是一个静态属性,没有对应的对象,传入null;第二个参数只有在属性是索引器是才有必要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值