列出目录并获取目录名称

发布于 2024-08-29 16:06:19 字数 827 浏览 2 评论 0原文

我试图获取代码来列出文件夹中的所有目录,将目录更改为该文件夹并获取当前文件夹的名称。到目前为止,我的代码如下,目前无法运行。我似乎正在获取父文件夹名称。

import os

for directories in os.listdir(os.getcwd()): 
    dir = os.path.join('/home/user/workspace', directories)
    os.chdir(dir)
    current = os.path.dirname(dir)
    new = str(current).split("-")[0]
    print new

我的文件夹中还有其他文件,但我不想列出它们。我已经尝试过下面的代码,但我还没有让它工作。

for directories in os.path.isdir(os.listdir(os.getcwd())): 

谁能看到我哪里出错了?

谢谢

,它可以工作,但似乎有点绕。

import os
os.chdir('/home/user/workspace')
all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
    dir = os.path.join('/home/user/workspace', dirs)
    os.chdir(dir)
    current = os.getcwd()
    new = str(current).split("/")[4]
    print new

I am trying to get the code to list all the directories in a folder, change directory into that folder and get the name of the current folder. The code I have so far is below and isn't working at the minute. I seem to be getting the parent folder name.

import os

for directories in os.listdir(os.getcwd()): 
    dir = os.path.join('/home/user/workspace', directories)
    os.chdir(dir)
    current = os.path.dirname(dir)
    new = str(current).split("-")[0]
    print new

I also have other files in the folder but I do not want to list them. I have tried the below code but I haven't got it working yet either.

for directories in os.path.isdir(os.listdir(os.getcwd())): 

Can anyone see where I am going wrong?

Thanks

Got it working but it seems a bit round about.

import os
os.chdir('/home/user/workspace')
all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
    dir = os.path.join('/home/user/workspace', dirs)
    os.chdir(dir)
    current = os.getcwd()
    new = str(current).split("/")[4]
    print new

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

南汐寒笙箫 2024-09-05 16:06:19

这将打印当前目录的所有子目录:

print [name for name in os.listdir(".") if os.path.isdir(name)]

我不确定您使用 split("-") 做什么,但也许这段代码可以帮助您找到解决方案?

如果您想要目录的完整路径名,请使用 abspath

print [os.path.abspath(name) for name in os.listdir(".") if os.path.isdir(name)]

请注意,这些代码片段只会获取直接子目录。如果您想要子子目录等,您应该按照其他人的建议使用 walk

This will print all the subdirectories of the current directory:

print [name for name in os.listdir(".") if os.path.isdir(name)]

I'm not sure what you're doing with split("-"), but perhaps this code will help you find a solution?

If you want the full pathnames of the directories, use abspath:

print [os.path.abspath(name) for name in os.listdir(".") if os.path.isdir(name)]

Note that these pieces of code will only get the immediate subdirectories. If you want sub-sub-directories and so on, you should use walk as others have suggested.

吃不饱 2024-09-05 16:06:19

对于Python的新版本

我喜欢@RichieHindle的答案,但我为其添加了一个小修复,

import os

folder = './my_folder'

sub_folders = [name for name in os.listdir(folder) if os.path.isdir(os.path.join(folder, name))]

print(sub_folders)

否则它对我来说并不真正有用

For New Versions of Python

I liked the answer of @RichieHindle but I add a small fix for it

import os

folder = './my_folder'

sub_folders = [name for name in os.listdir(folder) if os.path.isdir(os.path.join(folder, name))]

print(sub_folders)

otherwise it's not really work for me

绿萝 2024-09-05 16:06:19
import os
for root, dirs, files in os.walk(top, topdown=False):
    for name in dirs:
        print os.path.join(root, name)

Walk 是您正在做的事情的一个很好的内置功能

import os
for root, dirs, files in os.walk(top, topdown=False):
    for name in dirs:
        print os.path.join(root, name)

Walk is a good built-in for what you are doing

清欢 2024-09-05 16:06:19

您似乎正在使用 Python,就好像它是 shell 一样。每当我需要做类似你正在做的事情时,我都会使用 os.walk()

例如,如所解释的此处[x[0] for x in os.walk(directory)] 应该递归地为您提供所有子目录。

You seem to be using Python as if it were the shell. Whenever I've needed to do something like what you're doing, I've used os.walk()

For example, as explained here: [x[0] for x in os.walk(directory)] should give you all of the subdirectories, recursively.

向日葵 2024-09-05 16:06:19

列出当前目录中的条目(对于 os.listdir(os.getcwd()) 中的目录:),然后将这些条目解释为完全不同的目录的子目录 (dir = os. path.join('/home/user/workspace',directories)) 是一件看起来很可疑的事情。

Listing the entries in the current directory (for directories in os.listdir(os.getcwd()):) and then interpreting those entries as subdirectories of an entirely different directory (dir = os.path.join('/home/user/workspace', directories)) is one thing that looks fishy.

请止步禁区 2024-09-05 16:06:19

对 python3 的轻微修正(与@RichieHindle 相同的答案)

这将在数组中打印当前目录的所有子目录:

print( [name for name in os.listdir(".") if os.path.isdir(name)] )

为了使上面的内容更易于阅读

for name in os.listdir("."):
    if os.path.isdir(name):
        print(name)

如果您想要目录的完整路径名,请使用abspath:

print( [os.path.abspath(name) for name in os.listdir(".") if os.path.isdir(name)])

请注意,这些部分代码只会获取直接子目录。

Slight correction for python3 (same answer as @RichieHindle)

This will print all the subdirectories of the current directory in an array:

print( [name for name in os.listdir(".") if os.path.isdir(name)] )

To make the above simpler to read

for name in os.listdir("."):
    if os.path.isdir(name):
        print(name)

If you want the full pathnames of the directories, use abspath:

print( [os.path.abspath(name) for name in os.listdir(".") if os.path.isdir(name)])

Note that these pieces of code will only get the immediate subdirectories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文