目录中的 Python 文件夹名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29206384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python folder names in the directory
提问by HightronicDesign
how can i get the folder names existing in a directory using Python ?
如何使用 Python 获取目录中存在的文件夹名称?
I want to save all the subfolders into a list to work with the names after that but i dont know how to read the subfolder names ?
我想将所有子文件夹保存到一个列表中以处理之后的名称,但我不知道如何读取子文件夹名称?
Thanks for you help
谢谢你的帮助
采纳答案by Christian Eichelmann
You can use os.walk()
您可以使用 os.walk()
# !/usr/bin/python
import os
directory_list = list()
for root, dirs, files in os.walk("/path/to/your/dir", topdown=False):
for name in dirs:
directory_list.append(os.path.join(root, name))
print directory_list
EDIT
编辑
If you only want the first level and not actually "walk" through the subdirectories, it is even less code:
如果您只想要第一级而不是真正“遍历”子目录,则代码更少:
import os
root, dirs, files = os.walk("/path/to/your/dir").next()
print dirs
This is not really what os.walk
is made for. If you really only want one level of subdirectories, you can also use os.listdir()
like Yannik Ammann suggested:
这不是真正的os.walk
目的。如果你真的只想要一级子目录,你也可以os.listdir()
像 Yannik Ammann 建议的那样使用:
root='/path/to/my/dir'
dirlist = [ item for item in os.listdir(root) if os.path.isdir(os.path.join(root, item)) ]
print dirlist
回答by yamm
You can use os.listdir()
here a link to the docs
您可以os.listdir()
在此处使用指向文档的链接
Warning returns files and directories
警告返回文件和目录
example:
例子:
import os
path = 'pyth/to/dir/'
dir_list = os.listdir(path)
update:you need to check if the returned names are directories or files
更新:您需要检查返回的名称是目录还是文件
import os
path = 'pyth/to/dir/'
# list of all content in a directory, filtered so only directories are returned
dir_list = [directory for directory in os.listdir(path) if os.path.isdir(path+directory)]
回答by srekcahrai
Use os.walk(path)
用 os.walk(path)
import os
path = 'C:\'
for root, directories, files in os.walk(path):
for directory in directories:
print os.path.join(root, directory)
回答by poke
回答by titto.sebastian
You should import os first.
您应该先导入 os。
import os
files=[]
files = [f for f in sorted(os.listdir(FileDirectoryPath))]
This would give you list with all files in the FileDirectoryPath sorted.
这将为您提供在 FileDirectoryPath 中排序的所有文件的列表。
回答by oxidworks
I use os.listdir
我使用os.listdir
Get all folder names of a directory
获取目录的所有文件夹名称
folder_names = []
for entry_name in os.listdir(MYDIR):
entry_path = os.path.join(MYDIR, entry_name)
if os.path.isdir(entry_path):
folder_names.append(entry_name)
Get all folder paths of a directory
获取目录的所有文件夹路径
folder_paths = []
for entry_name in os.listdir(MYDIR):
entry_path = os.path.join(MYDIR, entry_name)
if os.path.isdir(entry_path):
folder_paths.append(entry_path)
Get all file names of a directory
获取目录的所有文件名
file_names = []
for file_name in os.listdir(MYDIR):
file_path = os.path.join(MYDIR, file_name)
if os.path.isfile(file_path):
file_names.append(file_name)
Get all file paths of a directory
获取目录的所有文件路径
file_paths = []
for file_name in os.listdir(MYDIR):
file_path = os.path.join(MYDIR, file_name)
if os.path.isfile(file_path):
file_paths.append(file_path)
回答by Traveler_3994
Python 3.x: If you want only the directories in a given directory, try:
Python 3.x:如果您只想要给定目录中的目录,请尝试:
import os
search_path = '.' # set your path here.
root, dirs, files = next(os.walk(search_path), ([],[],[]))
print(dirs)
The above example will print out a list of the directories in the current directory like this:
上面的例子将打印出当前目录中的目录列表,如下所示:
['dir1', 'dir2', 'dir3']
The output contains only the sub-directory names.
If the directory does not have sub-directories, it will print:
输出仅包含子目录名称。
如果目录没有子目录,它会打印:
[]
os.walk()is a generator method, so use next()to only call it once. The 3-tuple of empty strings is for the error condition when the directory does not contain any sub-directories because the os.walk() generator returns 3-tuples for each layer in the directory tree. Without those, if the directory is empty, next() will raise a StopIteration exception.
os.walk()是一个生成器方法,所以使用next()只调用一次。空字符串的三元组用于当目录不包含任何子目录时的错误情况,因为 os.walk() 生成器为目录树中的每一层返回三元组。如果没有这些,如果目录为空,next() 将引发 StopIteration 异常。
For a more compact version:
对于更紧凑的版本:
dirs = next(os.walk(search_path), ([],[],[]))[1]
回答by Tech
For python 3 I'm using this script
对于 python 3,我正在使用这个脚本
import os
root='./'
dirlist = [ item for item in os.listdir(root) if os.path.isdir(os.path.join(root, item)) ]
for dir in dirlist:
print(dir)