-1

I was looking for something and came across this:

subfolders = [ f.path for f in os.scandir(x) if f.is_dir() ]

I've never seen this type of conditional statement before.

What would you call this type of conditional statement to the right of the = sign? Just asking so I know what to Google.

Also, if I wanted to write the equivalent in another format, would it look something like this?

subfolders = []

x = "c:\\Users\\my_account\\AppData\\Program\\Cache"

for f in os.scandir(x):
     if f.isdir():
          print(f.x)

Would both of those statements be equivalent?

Tomerikoo
  • 12,112
  • 9
  • 27
  • 37
LunchBox
  • 2,179
  • 1
  • 18
  • 39
  • It does answer my question, but I want to try to create the equivalent. I'm going to edit my post. – LunchBox Apr 05 '21 at 23:17
  • 1
    You also have the answer for that in the link... – Tomerikoo Apr 05 '21 at 23:18
  • Does my edit look equivalent with the first set of code? If not, what should I have done differently. – LunchBox Apr 05 '21 at 23:18
  • Those statements will not be equivalent because the second one doesn't create a list... – Tomerikoo Apr 05 '21 at 23:19
  • I saw, but I haven't seen this before and just wanted someone to clarify before the post gets closed and I have to repost my question. – LunchBox Apr 05 '21 at 23:19
  • 1
    The answers to all your questions are either in that link or in the many many other resources available online about list-comprehension so there is nothing to repost – Tomerikoo Apr 05 '21 at 23:21
  • I edited my post, any suggestions? – LunchBox Apr 05 '21 at 23:32
  • Have you read their [documenation](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions)? What part is unclear? – martineau Apr 05 '21 at 23:35
  • I did read the question, but unfortunately, my code still is not working and I have not written these type of statements before. While this did answer my initial question, I didn't get my follow up question answered in the edit, so reposting that in a second. – LunchBox Apr 05 '21 at 23:36
  • 1
    This site is not designed for back-and-forth follow up questions and answers. I believe the question you started with has been answered, so you should create a new post if you have more questions. – John Kugelman Apr 05 '21 at 23:42
  • That's exactly what I did. – LunchBox Apr 05 '21 at 23:42
  • 1
    Hey, for what it's worth, closing a question doesn't mean it's bad or should be deleted. It's just the mechanism we have for saying, "I think your question is answered over here." Which it was, right? I know it can come across as harsh and I'm sorry for that. The nice thing about the duplicate system is that future googlers might find your question and be led to the canonical list comprehension Q&A, which I think is a benefit for all involved. – John Kugelman Apr 05 '21 at 23:58
  • 1
    I really hope you wouldn't feel that way. There is nothing not inviting in what happened here. Please note that your question is not simply `[closed]`. It is marked as `[duplicate]` which is a special-kind of closed. It just means that it is already answered, which you agreed on. So I really don't understand why say this is unwelcoming. This site is meant to be a quality library of programming questions and answers. Having the same question multiple times doesn't make a quality repository... – Tomerikoo Apr 06 '21 at 07:33

1 Answers1

1

Can't put code in a comment but here's the small change to your code to create equivalent output:

subfolders = []
for f in os.scandir(path):
     if f.is_dir():
          subfolders.append(f.path)

Here's the exact thing that worked in the interactive python for me:

Python 3.7.10 (default, Mar 24 2021, 16:34:34)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import os
>>> subfolders = []
>>> for f in os.scandir('.'):
...   if f.is_dir():
...     subfolders.append(f.path)
...
>>> subfolders
['./the', './.directories', './were', './here']

Checking in a newer python:

Python 3.9.2 (default, Mar 26 2021, 15:28:17)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> subfolders = []
>>> for f in os.scandir('.'):
...   if f.is_dir():
...     subfolders.append(f.path)
...
>>> subfolders
['./directory']
Ryan
  • 867
  • 7
  • 19