Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打印第二个文件夹名_Python_Python 3.x - Fatal编程技术网

Python 打印第二个文件夹名

Python 打印第二个文件夹名,python,python-3.x,Python,Python 3.x,我有一个Python代码,可以对文件夹中的文件夹进行排序。 但是,我想打印第二个文件夹的名称,而不是所有文件夹的名称。 有什么建议吗 for root,dirs,files in os.walk("C:\\Folder testing"): for dirname in sorted(dirs, key=int, reverse=True): print(dirs) 我不会使用os.walk只打印一个文件夹。 我希望列出所有文件夹,然后选择我想要的: some_path = "

我有一个Python代码,可以对文件夹中的文件夹进行排序。 但是,我想打印第二个文件夹的名称,而不是所有文件夹的名称。 有什么建议吗

for root,dirs,files in os.walk("C:\\Folder testing"):
   for dirname in sorted(dirs, key=int, reverse=True):
     print(dirs)

我不会使用os.walk只打印一个文件夹。 我希望列出所有文件夹,然后选择我想要的:

some_path = "C:\\Folder testing"
dirs = [f for f in os.listdir(some_path) if os.path.isdir(os.path.join(some_path, f))]
dirs_sorted = sorted(dirs, key=int, reverse=True)
try: 
    print dirs_sorted[1]
except IndexError:
    print "Folder doesn't exist"

请注意,您的排序方法要求文件夹名称仅为数字。

能否详细说明您的问题?第二个文件夹来自何处?正如我所看到的,你必须列出所有文件夹的列表,然后打印出来。我有一个文件夹“folder testing”,我只想打印其中一个文件夹(在本例中是第二个文件夹)的名称,而不是所有文件夹如果只想打印一个文件夹,那么为什么要使用os.walk?因为首先我想按数字顺序组织它们。这就是为什么会有一个排序。好的,然后用你的文件夹列一个列表,然后对列表排序,然后打印你想要的文件夹。不起作用。给出错误:“索引器错误:列表索引超出范围”,因为该目录为空。该目录不为空。我试图在try循环之前打印列表,但它返回空。(是的,应该是数字)