0
anyone please explain second line of this code....
for dirpath, dirnames, filenames in os.walk(r"C:\Users\Documents\MP3"): for filename in [f for f in filenames if f.endswith(".mp3")]: print(filename) anyone please explain...why two times "f" is required inside the square brackets(f for f) ... thank you in advance:)
2 ответов
+ 4
That's a list comprehension. [x for x in list] creates a new list and adds all elements of "list" without any changes. If you use something like [x.upper() for x in list], the list will contain an uppercase version of each element, [x[0] for x in list] will only contain the first letter of each element etc.
Your example will create a list of all file names in the specified folder that end with '.mp3'.
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2454/
+ 1
thank you very much Anna