Python不返回目录值

标签 python

我正在尝试迭代目录并输出当前项是否是文件或目录。这是我尝试使用的代码

import os,time

dir = os.listdir("V:\\Inbox/")

for item in dir:
    if os.path.isdir(item):
        print "is Directory"
    else:
        print "is file"

最佳答案

您需要将基本目录添加到路径中,然后再将其传递给 isdir

import os

basedir = "V:/Inbox/"
dir = os.listdir(basedir)

for item in dir:
    itempath = os.path.join(basedir, item)
    if os.path.isdir(itempath):
        print "is Directory"
    else:
        print "is file"

顺便说一句,通常在发布到 SO 之前做一些 print 语句是一件好事(尽管我不会对这些问题的简单代表说不:P)。

关于Python不返回目录值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9278562/

相关文章:

python - 使用 np.where 根据某个值增加计数器

python - 如何从图像的背景中删除感兴趣的区域?

python - Tensorflow - 带有 sample_weight 的自定义损失函数

python - 使用 `waitress` 在多个端口上提供 Django 应用程序

Python:按名称获取 smartsheet Sheet 的最佳方法

python - Moose 与 Python 的 OO 系统相比如何?

python - azure 管道中的 pip install azure-functions 因 pip 验证任务而失败

python - Pandas : default columns names

python - 使用Python删除目录中的所有文件

python - 剪切零字符(字符串[:-0]) from string - Python