目录列表 [英] directory listing

查看:45
本文介绍了目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经挣扎了很长一段时间而且我只是不确定

发生了什么。我有以下代码

import os

def buildList(directory =''/ Users / mkonrad'')


dirs = []


listing = os.listdir(directory)


for x in listing:

如果os.path.isdir(x):

dirs.append(x)


返回dirs


这总是返回一个空列表。

现在,如果我更改它,以便directory =''。''或directory = os.getcwd()

然后它返回一个目录列表。


任何想法?


谢谢,

-Michael


解决方案

" SU News Server" < MK ***** @ syr.edu>写道:

我已经挣扎了很长一段时间,我只是不确定
发生了什么。我有以下代码
导入os


dirs = []

listing = os.listdir(目录)
for x in listing:
如果os.path.isdir(x):
dirs.append(x)
这总是返回一个空列表。
现在,如果我更改它,以便directory =''。''或directory = os.getcwd()<然后它返回一个目录列表。




提示:如果你不确定程序中发生了什么,添加

a打印一两句是一个简单的解决方法。比如说:


for x in listing:

print x

if os.path.isdir(x):

dirs.append(x)


(在继续之前试试这个)









问题是os.listdir返回文件名列表,没有

前面的目录名称。你可以在列表中为x添加一个os.path.join




x = os.path.join(目录,x)

如果os.path.isdir(x):

dirs.append(x)


或使用glob模块代替:


listing = glob.glob(os.path.join(目录," *"))


希望这有帮助!


< / F>


2005年11月11日21:20:33 GMT,SU News Server写道:


尝试将每个项目的完整路径名传递给os.path.isdir()


您可以使用os.path.join创建路径名(目录,x)

-

Richard


" Fredrik Lundh" < FR ***** @ pythonware.com>写道:

" SU News Server" < MK ***** @ syr.edu>写道:

我已经挣扎了很长一段时间,我只是不确定
发生了什么。我有以下代码
导入os


dirs = []

listing = os.listdir(目录)
for x in listing:
如果os.path.isdir(x):
dirs.append(x)
这总是返回一个空列表。
现在,如果我更改它,以便directory =''。''或directory = os.getcwd()<然后它返回一个目录列表。
提示:如果您不确定程序中发生了什么,添加一两个打印语句是一种简单的方法出来。比如说:

for x in listing:
print x
if os.path.isdir(x):
dirs.append(x)



那个和我刚收到一堆[]。

(在你继续之前试试这个)





问题是os.listdir返回一个文件名列表,没有
前面的目录名。你可以在列表中添加一个os.path.join


x = os.path.join(目录,x)
如果是os.path.isdir(x ):
dirs.append(x)


好​​的。这是有效的,但目前dirs中的每个条目都包含完整路径。

有没有加入? :)我没有花时间尝试解决这个问题。

或者使用glob模块:

listing = glob.glob(os.path.join (目录," *"))

希望这会有所帮助!

< / F>



I''ve struggled with this for quite a while and I''m am just not sure
what is going on. I have the following code
import os

def buildList( directory=''/Users/mkonrad'' )

dirs = [ ]

listing = os.listdir(directory)

for x in listing:
if os.path.isdir(x):
dirs.append(x)

return dirs

This always returns an empty list.
Now if I change it so that directory=''.'' or directory=os.getcwd()
Then it returns a list of directories.

Any ideas?

Thank you,
-Michael


解决方案

"SU News Server" <mk*****@syr.edu> wrote:

I''ve struggled with this for quite a while and I''m am just not sure
what is going on. I have the following code
import os

def buildList( directory=''/Users/mkonrad'' )

dirs = [ ]

listing = os.listdir(directory)

for x in listing:
if os.path.isdir(x):
dirs.append(x)

return dirs

This always returns an empty list.
Now if I change it so that directory=''.'' or directory=os.getcwd()
Then it returns a list of directories.



hint: if you''re not sure what''s going on in your program, adding
a print statement or two is an easy way to figure it out. like, say:

for x in listing:
print x
if os.path.isdir(x):
dirs.append(x)

(try this before you continue)

:
:
:

the problem is that os.listdir returns a list of filenames, without
the preceeding directory name. you can add an os.path.join

for x in listing:
x = os.path.join(directory, x)
if os.path.isdir(x):
dirs.append(x)

or use the glob module instead:

listing = glob.glob(os.path.join(directory, "*"))

hope this helps!

</F>


On 11 Nov 2005 21:20:33 GMT, SU News Server wrote:

Try passing the full pathname of each item to os.path.isdir()

You can create the pathname using os.path.join(directory, x)
--
Richard


"Fredrik Lundh" <fr*****@pythonware.com> wrote:

"SU News Server" <mk*****@syr.edu> wrote:

I''ve struggled with this for quite a while and I''m am just not sure
what is going on. I have the following code
import os

def buildList( directory=''/Users/mkonrad'' )

dirs = [ ]

listing = os.listdir(directory)

for x in listing:
if os.path.isdir(x):
dirs.append(x)

return dirs

This always returns an empty list.
Now if I change it so that directory=''.'' or directory=os.getcwd()
Then it returns a list of directories.
hint: if you''re not sure what''s going on in your program, adding
a print statement or two is an easy way to figure it out. like, say:

for x in listing:
print x
if os.path.isdir(x):
dirs.append(x)



Did that and I was just getting a bunch of [ ].
(try this before you continue)

:
:
:

the problem is that os.listdir returns a list of filenames, without
the preceeding directory name. you can add an os.path.join

for x in listing:
x = os.path.join(directory, x)
if os.path.isdir(x):
dirs.append(x)
OK. This works except each entry in dirs now includes the full path.
Is there an unjoin? :) I haven''t spent any time trying to work this out.

or use the glob module instead:

listing = glob.glob(os.path.join(directory, "*"))

hope this helps!

</F>




这篇关于目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆