Getting a list of all subdirectories in the current directory

Is there a way to return a list of all the subdirectories in the current directory in Python? I know you can do this with files, but I need to get the list of directories instead. Do you mean immediate subdirectories, or every directory right down the tree? Either way, you could use os.walk to do this: os.walk(directory) will yield a tuple for each subdirectory. Ths first entry in the 3-

获取当前目录中所有子目录的列表

有没有办法在Python中返回当前目录中所有子目录的列表? 我知道你可以用文件做到这一点,但我需要获取目录列表。 你的意思是直接的子目录,或者树上的每个目录? 无论哪种方式,你可以使用os.walk来做到这一点: os.walk(directory) 会为每个子目录产生一个元组。 三元组中的第一个条目是一个目录名称,所以 [x[0] for x in os.walk(directory)] 应该递归地给你所有的子目录。 请注意,元组中的第二个条目是第一个

Most elegant way to check if the string is empty in Python?

Does Python have something like an empty string variable where you can do?: if myString == string.empty: Regardless what's the most elegant way to check for empty string values? I find hard coding "" every time for checking an empty string not as good. Empty strings are "falsy" which means they are considered false in a Boolean context, so you can just do this: if no

最优雅的方法来检查在Python中的字符串是否为空?

Python是否有类似空字符串变量的地方? if myString == string.empty: 无论检查空字符串值的最优雅方式是什么? 我发现硬编码""每次检查一个空字符串都不好。 空字符串是“虚假”,这意味着它们在布尔上下文中被认为是错误的,所以你可以这样做: if not myString: 如果你知道你的变量是一个字符串,这是首选的方法。 如果你的变量也可以是其他类型,那么你应该使用myString == "" 。 有关在布尔上下

How to clone or copy a list?

What are the options to clone or copy a list in Python? Using new_list = my_list then modifies new_list every time my_list changes. Why is this? With new_list = my_list , you don't actually have two lists. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the assignment. To actually copy the list, you

如何克隆或复制列表?

在Python中克隆或复制列表有哪些选项? 每次my_list更改时,使用new_list = my_list然后修改new_list 。 为什么是这样? 用new_list = my_list ,你实际上没有两个列表。 分配只是将引用复制到列表中,而不是实际列表,因此new_list和my_list在分配后引用同一个列表。 要真正复制列表,您有各种可能性: 你可以切片: new_list = old_list[:] 亚历克斯马尔泰利的观点(至少在2007年)是这样的,它是一种奇怪的语法

How to concatenate two lists in Python?

How do I concatenate two lists in Python? Example: listone = [1, 2, 3] listtwo = [4, 5, 6] Expected outcome: >>> joinedlist [1, 2, 3, 4, 5, 6] You can use the + operator to combine them: listone = [1,2,3] listtwo = [4,5,6] mergedlist = listone + listtwo Output: >>> mergedlist [1,2,3,4,5,6] It's also possible to create a generator that simply iterates over the item

如何在Python中连接两个列表?

我如何在Python中连接两个列表? 例: listone = [1, 2, 3] listtwo = [4, 5, 6] 预期结果: >>> joinedlist [1, 2, 3, 4, 5, 6] 您可以使用+运算符来合并它们: listone = [1,2,3] listtwo = [4,5,6] mergedlist = listone + listtwo 输出: >>> mergedlist [1,2,3,4,5,6] 也可以创建一个生成器,对这两个列表中的项目进行迭代。 这允许您将列表(或任何可迭代的)链接在一起进行处理,而无需将

How to get the number of elements in a list in Python?

items = [] items.append("apple") items.append("orange") items.append("banana") # FAKE METHOD:: items.amount() # Should return 3 我如何获得列表中的元素数量? len()函数可以与Python中的很多类型一起使用 - 包括内置类型和库类型。 >>> len([1,2,3]) 3 How to get the size of a list? To find the size of a list, use the builtin function, len : items = [] items.append("apple") items.append("ora

如何获取Python中列表中元素的数量?

items = [] items.append("apple") items.append("orange") items.append("banana") # FAKE METHOD:: items.amount() # Should return 3 我如何获得列表中的元素数量? len()函数可以与Python中的很多类型一起使用 - 包括内置类型和库类型。 >>> len([1,2,3]) 3 如何获得列表的大小? 要查找列表的大小,请使用内建函数len : items = [] items.append("apple") items.append("orange") items.append("banana")

Making a flat list out of list of lists in Python

I wonder whether there is a shortcut to make a simple list out of list of lists in Python. I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with reduce, but I get an error. Code l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(lambda x, y: x.extend(y), l) Error message Traceback (most recent call last): File "<stdin>", line 1, in <module

从Python列表中列出一个扁平列表

我想知道是否有一个快捷方式可以在Python列表中列出一个简单的列表。 我可以在for循环中做到这一点,但也许有一些很酷的“单线程”? 我尝试减少,但我得到一个错误。 码 l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(lambda x, y: x.extend(y), l) 错误信息 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <lambda> AttributeError

Getting the last element of a list in Python

在Python中,你如何得到列表的最后一个元素? some_list[-1] is the shortest and most Pythonic. In fact, you can do much more with this syntax. The some_list[-n] syntax gets the nth-to-last element. So some_list[-1] gets the last element, some_list[-2] gets the second to last, etc, all the way down to some_list[-len(some_list)] , which gives you the first element. You can also set list elements

在Python中获取列表的最后一个元素

在Python中,你如何得到列表的最后一个元素? some_list[-1]是最短和最Pythonic。 事实上,你可以用这个语法做更多的事情。 some_list[-n]语法获取倒数第n个元素。 所以some_list[-1]得到最后一个元素, some_list[-2]得到倒数第二个等等,一直到some_list[-len(some_list)] ,这会给你第一个元素。 您也可以用这种方式设置列表元素。 例如: >>> some_list = [1, 2, 3] >>> some_list[-1] = 5 # Set t

How to remove an element from a list by index in Python?

How to remove an element from a list by index in Python? I found the list.remove method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed. Use del and specify the element you want to delete with the index: In [9]: a = list(range(10)) In [10]: a Out[10]: [0, 1, 2, 3, 4, 5, 6, 7, 8,

如何通过Python中的索引从列表中删除元素?

如何通过Python中的索引从列表中删除元素? 我找到了list.remove方法,但说我想删除最后一个元素,我该怎么做? 它似乎像默认删除搜索列表,但我不希望执行任何搜索。 使用del并用索引指定要删除的元素: In [9]: a = list(range(10)) In [10]: a Out[10]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [11]: del a[-1] In [12]: a Out[12]: [0, 1, 2, 3, 4, 5, 6, 7, 8] 以下是教程中的部分。 你可能想要pop : a = ['a', 'b',

Integer difference in python between two dates

I've RTFM and read many questions and answers here on SO regarding this, and was happily using strftime and strptime yesterday, so I would swear this should work, but it isn't.... I just want an integer. Not a "timedelta object." Not an "aware yet hashable object" (see, I RTFM). Not a tuple. Not a dictionary. Just a simple freaking integer so I can use an if sta

python在两个日期之间的整数差异

我有RTFM,在这里阅读了很多关于这个的问题和答案,昨天很高兴地使用strftime和strptime,所以我会发誓这应该工作,但它不是.... 我只想要一个整数。 不是“timedelta对象”。 不是一个“意识到但可排除的对象”(参见I RTFM)。 不是元组。 不是字典。 只是一个简单的怪异整数,所以我可以使用if语句和分支并且很高兴。 请带上您的智慧之光,谢谢。 这是我的 ... import datetime mdate = "2010-10-05" rdate = "2010-10-

Is a day always 86,400 epoch seconds long?

While reviewing my past answers, I noticed I'd proposed code such as this: import time def dates_between(start, end): # muck around between the 9k+ time representation systems in Python # now start and end are seconds since epoch # return [start, start + 86400, start + 86400*2, ...] return range(start, end + 1, 86400) When rereading this piece of code, I couldn't help but feel

一天总是有86,400个纪元秒吗?

在回顾过去的回答时,我注意到我提出了如下代码: import time def dates_between(start, end): # muck around between the 9k+ time representation systems in Python # now start and end are seconds since epoch # return [start, start + 86400, start + 86400*2, ...] return range(start, end + 1, 86400) 当重读这段代码时,我忍不住感到脊柱上托尼小马的可怕接触,轻轻地在我的耳朵和其他可怕的,可怕的事