2006年02月22日 星期三 16:38
为什么os.path.isfile没有起作用?
files = []
def visit(arg, dirname, names):
files = files + [os.path.abspath(name for name in names if
os.path.isfile(name)]
os.path.walk('c:\\windows', visit, None)
2006年02月22日 星期三 16:50
On 2/22/06, Julius F. Huang <fozzold at gmail.com> wrote: > 为什么os.path.isfile没有起作用? > > files = [] > > def visit(arg, dirname, names): > files = files + [os.path.abspath(name for name in names if > os.path.isfile(name)] > > os.path.walk('c:\\windows', visit, None) > 出问题的应该不是isfile,而是os.path.isfile(name)。 name的内容只是一个文件名,不包含路径,比方说"test.txt",而os.path.isfile()要指定文件路径的,不然默认就是当前路径了。 所以应该写成os.path.isfile(os.path.join(dirname, name)) -- Best Regards, Leo Jay
2006年02月22日 星期三 17:54
对,谢谢。 :) On 2/22/06, Leo Jay <python.leojay at gmail.com> wrote: > On 2/22/06, Julius F. Huang <fozzold at gmail.com> wrote: > > 为什么os.path.isfile没有起作用? > > > > files = [] > > > > def visit(arg, dirname, names): > > files = files + [os.path.abspath(name for name in names if > > os.path.isfile(name)] > > > > os.path.walk('c:\\windows', visit, None) > > > > 出问题的应该不是isfile,而是os.path.isfile(name)。 > name的内容只是一个文件名,不包含路径,比方说"test.txt",而os.path.isfile()要指定文件路径的,不然默认就是当前路径了。 > 所以应该写成os.path.isfile(os.path.join(dirname, name)) > > -- > Best Regards, > Leo Jay > > _______________________________________________ > 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 > >
2006年02月22日 星期三 22:44
你是想找出c:\windows下的所有文件吗?os.walk更容易使用一些
import os
import os.path
files_under_windows = []
for root, dirs, files in os.walk('c:\\windows'):
files_under_windows = files_under_windows + [os.path.join(root,
file) for file in files]
2006/2/22, Julius F. Huang <fozzold at gmail.com>:
> 为什么os.path.isfile没有起作用?
>
> files = []
>
> def visit(arg, dirname, names):
> files = files + [os.path.abspath(name for name in names if
> os.path.isfile(name)]
>
> os.path.walk('c:\\windows', visit, None)
>
> _______________________________________________
> 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
>
>
2006年02月23日 星期四 10:10
On 2/22/06, wolfg <wolfg1969 at gmail.com> wrote: > 你是想找出c:\windows下的所有文件吗?os.walk更容易使用一些 如果是简单的找文件的话,glob更容易使用。 import glob print glob.glob(r"c:\windows\*.ini") -- Best Regards, Leo Jay
2006年02月23日 星期四 13:57
谢谢各位。 :) On 2/22/06, Leo Jay <python.leojay at gmail.com> wrote: > On 2/22/06, wolfg <wolfg1969 at gmail.com> wrote: > > 你是想找出c:\windows下的所有文件吗?os.walk更容易使用一些 > > 如果是简单的找文件的话,glob更容易使用。 > import glob > print glob.glob(r"c:\windows\*.ini") > > -- > Best Regards, > Leo Jay > > _______________________________________________ > 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 > >
Zeuux © 2025
京ICP备05028076号