问题描述
# - * - 编码:utf-8 - * -
#Python
假设你想进入一个目录,比如申请一个字符串
替换所有html文件。 os.path.walk()上升为
场合。
?导入os
? mydir =''/ Users / t / Documents / unix_cilre / python''
? def myfun(s1,s2,s3):
? print s2#current dir
?打印s3#那里的文件列表
?打印''------ ==(^ _ ^)== ------''
? os.path.walk(mydir,myfun,''somenull'')
----------------------
os.path.walk(base_dir,f,arg)将从
base_dir开始走向一棵目录树,每当它看到一个目录(包括base_dir)时,它就会
将调用f(arg,current_dir,children),其中current_dir是当前目录的
字符串,children是* list * of all
当前目录的子项。也就是说,一个字符串列表是
文件名和目录名。试试上面的内容,你会看到。
现在,假设每个以.html结尾的文件我们想要应用函数
g。因此,当调用myfun时,我们需要循环通过
子列表,查找文件并以html结尾(而不是目录),
然后调用g。这是代码。
?导入os
? mydir =''/ Users / t / web / SpecialPlaneCurves_dir''
? def g(s):printg touch:,s
? def myfun(dummy,dirr,filess):
?对于文件中的孩子:
? if''.html''== os.path.splitext(child)[1] \
?和os.path.isfile(dirr +''/''+孩子):
? g(dirr +孩子)
? os.path.walk(mydir,myfun,3)
请注意os.path.splitext将一个字符串分成两部分,一部分
之前最后一个时期,其余部分在第二部分。有效地
它用于获取文件后缀。并且os.path.isfile()确保
这是一个文件而不是带有.html后缀的目录...自己测试一下。
需要注意的一件重要事情是:在mydir中,它不能以
斜线结束。一个人会认为Python会照顾这样的琐事而不是。这需要一段时间来调试。
,os.path.walk的语义方式也很好。 myfun可以
是一个递归函数,调用自身,结晶程序'
语义。
--- ------------------------
#在Perl中,可以有类似的程序。
#遍历目录的典型方式
#通过文件::查找;
使用文件::查找qw(查找);
$ mydir =''/ Users / t / web / SpecialPlaneCurves_dir'';
find(\& wanted,$ mydir);
sub g ($){print shift," \ n";}
sub想要{
if($ _ =〜/ \。$ /&& ; -T $ File :: Find :: name){g $ File :: Find :: name;}
$ File :: Find :: name;
}
#上面展示了一个快速黑客。
#File ::查找是最糟糕的模块之一
#有在Perl。一个人不能使用它带有递归(所谓的)过滤器
#函数的
#。而且因为它的编写方式是#b
#,所以无法制作过滤器
#功能纯粹的功能。 (它在$ _上依赖
#)并且过滤功能
#必须按特定顺序排列。 (对于
#例子,上面的程序不会工作
#如果g被移到底部。)...
#Perl模块的质量是
#都是这样的。
Xah
# -*- coding: utf-8 -*-
# Python
suppose you want to walk into a directory, say, to apply a string
replacement to all html files. The os.path.walk() rises for the
occasion.
? import os
? mydir= ''/Users/t/Documents/unix_cilre/python''
? def myfun(s1, s2, s3):
? print s2 # current dir
? print s3 # list of files there
? print ''------==(^_^)==------''
? os.path.walk(mydir, myfun, ''somenull'')
----------------------
os.path.walk(base_dir,f,arg) will walk a dir tree starting at
base_dir, and whenever it sees a directory (including base_dir), it
will call f(arg,current_dir,children), where the current_dir is the
string of the current directory, and children is a *list* of all
children of the current directory. That is, a list of strings that are
file names and directory names. Try the above and you''ll see.
now, suppose for each file ending in .html we want to apply function
g to it. So, when ever myfun is called, we need to loop thru the
children list, find files and ending in html (and not a directory),
then call g. Here''s the code.
? import os
? mydir= ''/Users/t/web/SpecialPlaneCurves_dir''
? def g(s): print "g touched:", s
? def myfun(dummy, dirr, filess):
? for child in filess:
? if ''.html'' == os.path.splitext(child)[1] \
? and os.path.isfile(dirr+''/''+child):
? g(dirr+child)
? os.path.walk(mydir, myfun, 3)
note that os.path.splitext splits a string into two parts, a portion
before the last period, and the rest in the second portion. Effectively
it is used for getting file suffix. And the os.path.isfile() make sure
that this is a file not a dir with .html suffix... Test it yourself.
one important thing to note: in the mydir, it must not end in a
slash. One''d think Python''d take care of such trivia but no. This took
me a while to debug.
also, the way of the semantics of os.path.walk is nice. The myfun can
be a recursive function, calling itself, crystalizing a program''s
semantic.
---------------------------
# in Perl, similar program can be had.
# the prototypical way to traverse a dir
# is thru File::Find;
use File::Find qw(find);
$mydir= ''/Users/t/web/SpecialPlaneCurves_dir'';
find(\&wanted, $mydir);
sub g($){print shift, "\n";}
sub wanted {
if ($_ =~/\.html$/ && -T $File::Find::name) { g $File::Find::name;}
$File::Find::name;
}
# the above showcases a quick hack.
# File::Find is one of the worst module
# there is in Perl. One cannot use it
# with a recursive (so-called) "filter"
# function. And because the way it is
# written, one cannot make the filter
# function purely functional. (it relies
# on the $_) And the filter function
# must come in certain order. (for
# example, the above program won''t work
# if g is moved to the bottom.) ...
# the quality of modules in Perl are
# all like that.
Xah
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html
推荐答案
这篇关于[perl-python] 20050127遍历一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!