2006年11月14日 星期二 17:53
只使用os模块,比如一个目录下面可能还有N级目录,我要全部遍历出来,然后把所有文件保存到一个列表中 其实主要想知道有什么方法可以判断一个名字是目录还是文件。 谢谢各位。。。
2006年11月14日 星期二 17:56
There is a function isdir()? in os module, I think. Sent from my BlackBerry® wireless handheld -----Original Message----- From: WhiteFox <wyh817在gmail.com> Date: Tue, 14 Nov 2006 17:53:54 To:python-chinese在lists.python.cn Subject: [python-chinese] 我想问一下,如何遍 历整个目录阿 只使用os模块,比如一个目录下面可能还有N级目录,我要全部遍历出来,然后把所有文件保存到一个列表中 其实主要想知道有什么方法可以判断一个名字是目录还是文件。 谢谢各位。。。 _______________________________________________ python-chinese Post: send python-chinese在lists.python.cn Subscribe: send subscribe to python-chinese-request在lists.python.cn Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn Detail Info: http://python.cn/mailman/listinfo/python-chinese
2006年11月14日 星期二 17:57
os.path.walk 2006/11/14, WhiteFox <wyh817在gmail.com>: > > ֻʹÓÃosÄ£¿é£¬±ÈÈçÒ»¸öĿ¼ÏÂÃæ¿ÉÄÜ»¹ÓÐN¼¶Ä¿Â¼£¬ÎÒҪȫ²¿±éÀú³öÀ´£¬È»ºó°ÑËùÓÐÎļþ±£´æµ½Ò»¸öÁбíÖÐ > > ÆäʵÖ÷ÒªÏëÖªµÀÓÐʲô·½·¨¿ÉÒÔÅжÏÒ»¸öÃû×ÖÊÇĿ¼»¹ÊÇÎļþ¡£ > > лл¸÷λ¡£¡£¡£ > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- »¶Ó·ÃÎÊ£º http://blog.csdn.net/ccat ÁõöÎ March.Liu -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20061114/95b99b08/attachment.html
2006年11月15日 星期三 10:52
if not os.path.exists(path):
print "错误:%s不存在!"%path
return "0"
else:
for f in os.listdir(path):
if os.path.isdir(os.path.join(path, f)):
# 处理文件夹
……
else:
# 处理文件 f
…………
On 11/14/06, 刘鑫 <march.liu at gmail.com> wrote:
>
> os.path.walk
>
> 2006/11/14, WhiteFox <wyh817 at gmail.com>:
> >
> > 只使用os模块,比如一个目录下面可能还有N级目录,我要全部遍历出来,然后把所有文件保存到一个列表中
> >
> > 其实主要想知道有什么方法可以判断一个名字是目录还是文件。
> >
> > 谢谢各位。。。
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
>
> --
> 欢迎访问:
> http://blog.csdn.net/ccat
>
> 刘鑫
> March.Liu
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20061115/4e0a546f/attachment.htm
2006年11月15日 星期三 12:16
我用递归的方法实现了目录中所有文件的搜索(除了txt和doc),我不太会用os.path.walk这个函数。。
可能用那个速度更快一点
用python以后发现写程序的速度比以前用c不知道快了多少,而且条理也清楚很多了。
FileList=[]
def GetTheFileName(pathname):
subpathlist=os.listdir(pathname)
if subpathlist==[]: #如果是空目录
return 1
else:
for count in range(len(subpathlist)):
subpathlist[count]=os.path.join(pathname,subpathlist[count]) #完整路径
for count in range(len(subpathlist)):
if os.path.isdir(subpathlist[count])==True: #如果是目录
subpathlist[count]=subpathlist[count]+"\\"
GetTheFileName(subpathlist[count]) #递归搜索该目录
else: #如果是文件
if subpathlist[count][-3:]!="doc" and
subpathlist[count][-3:]!="txt": #如果不是doc和txt文件
FileList.append(subpathlist[count]) #加入到文件列表中
return 1
在 06-11-15,尹祥龙<yinxianglong在gmail.com> 写道:
> if not os.path.exists(path):
> print "错误:%s不存在!"%path
> return "0"
> else:
> for f in os.listdir(path):
> if os.path.isdir(os.path.join(path, f)):
> # 处理文件夹
> ……
> else:
> # 处理文件 f
> …………
>
>
> On 11/14/06, 刘鑫 <march.liu在gmail.com> wrote:
> > os.path.walk
> >
> >
> > 2006/11/14, WhiteFox <wyh817在gmail.com>:
> >
> > > 只使用os模块,比如一个目录下面可能还有N级目录,我要全部遍历出来,然后把所有文件保存到一个列表中
> > >
> > > 其实主要想知道有什么方法可以判断一个名字是目录还是文件。
> > >
> > > 谢谢各位。。。
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
> > --
> > 欢迎访问:
> > http://blog.csdn.net/ccat
> >
> > 刘鑫
> > March.Liu
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> >
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
2006年11月15日 星期三 12:32
from path import path
d = path(' /some_dir ')
files = list(d.walkfiles())
# ;-)
--
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20061115/ed527661/attachment.html
2006年11月15日 星期三 12:43
。。。。。。。。 三句话就搞定了。我写了一个函数,呵呵,谢谢楼上的兄弟。 2006/11/15, yi huang <yi.codeplayer在gmail.com>: > from path import path > d = path(' /some_dir ') > files = list(d.walkfiles()) > # ;-) > > -- > http://codeplayer.blogspot.com/ > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese >
2006年11月15日 星期三 20:45
你需要安装 path模块 <http://www.jorendorff.com/articles/python/path/>。不过考虑到这个模块的强大和优雅,这一点麻烦应该没关系的,况且它只有一个文件。期待它早日进入标准库的行列! os.path 可以下岗了! -- http://codeplayer.blogspot.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20061115/a4f3e5e7/attachment.htm
2006年11月15日 星期三 22:05
很赞赏你的做法。不过使用Python的库会更好。:) -- GoogleTalk: qcxhome at gmail.com MSN: qcxhome at hotmail.com My Space: tkdchen.spaces.live.com BOINC: boinc.berkeley.edu 中国分布式计算总站: www.equn.com
Zeuux © 2025
京ICP备05028076号