列出目录并获取目录名称
我试图获取代码来列出文件夹中的所有目录,将目录更改为该文件夹并获取当前文件夹的名称。到目前为止,我的代码如下,目前无法运行。我似乎正在获取父文件夹名称。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这将打印当前目录的所有子目录:
我不确定您使用 split("-") 做什么,但也许这段代码可以帮助您找到解决方案?
如果您想要目录的完整路径名,请使用
abspath
:请注意,这些代码片段只会获取直接子目录。如果您想要子子目录等,您应该按照其他人的建议使用
walk
。This will print all the subdirectories of the current directory:
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
: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.对于Python的新版本
我喜欢@RichieHindle的答案,但我为其添加了一个小修复,
否则它对我来说并不真正有用
For New Versions of Python
I liked the answer of @RichieHindle but I add a small fix for it
otherwise it's not really work for me
Walk 是您正在做的事情的一个很好的内置功能
Walk is a good built-in for what you are doing
您似乎正在使用 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.列出当前目录中的条目(
对于 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.对 python3 的轻微修正(与@RichieHindle 相同的答案)
这将在数组中打印当前目录的所有子目录:
为了使上面的内容更易于阅读
如果您想要目录的完整路径名,请使用abspath:
请注意,这些部分代码只会获取直接子目录。
Slight correction for python3 (same answer as @RichieHindle)
This will print all the subdirectories of the current directory in an array:
To make the above simpler to read
If you want the full pathnames of the directories, use abspath:
Note that these pieces of code will only get the immediate subdirectories.