0

The title may sound confusing so here is what I want...

I have the following code:

import os
dir_list = os.walk('aFolder').next()[1]     #aFolder is the directory location

SizeOfList =  len(dir_list)
NumbOfList = 0

while (NumbOfList < SizeOfList):
    print dir_list [NumbOfList]
    NumbOfList = NumbOfList + 1

This code gets all the folders (not including subfolders) in the folder called 'aFolder' and shows them to the user. However, I want to store these folder names as different variables. The problem I have here is, I don't know the number of folders in this directory so I can't set 1 variable to dir_list [0] and another to dir_list [1] etcetera.

I need this because I want to select and choose certain folders with an if statement. If you have an alternative to doing this than mine, I will happily accept it.

My solution to this was using a while loop. However, I can't figure out how to change the variable name each time the while loop repeats. I have tried this, this and this. The third link was the closest, but I still can't figure out how to implement it.

Thank you in advance to anyone who helps.

Community
  • 1
  • 1
Sank6
  • 491
  • 9
  • 28
  • 1
    Aren't you already storing them, in `dir_list`? – kabanus May 05 '17 at 16:46
  • @kabanus yes there is already a list. The first line after the import does that. – Sank6 May 05 '17 at 16:47
  • 3
    Why is the list not sufficient? Why do you need to create separate variables? This is just a bad idea from the start. – Mark Ransom May 05 '17 at 16:47
  • That's what I'm asking, why is the list variable not counting here as a variable...? You can access directory names easily afterwards. – kabanus May 05 '17 at 16:48
  • I would like them as separate variables so that I can pick with an `if` statement and select a certain directory. – Sank6 May 05 '17 at 16:50
  • 1
    Than write **that** in your question. Write exactly what you need afterwards. This is an [xy](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – kabanus May 05 '17 at 16:50
  • Ok will change now. – Sank6 May 05 '17 at 16:51
  • 1
    maybe you can use a dictionary, if you want to store some additional data for each folder. Like d=dict()......d.update({dir_list [NumbOfList] : data}) – dede May 05 '17 at 16:52
  • @dede could you expand. it sounds like the thing I would like. – Sank6 May 05 '17 at 16:53
  • even if you can (which you can, but it is really not recommended) get var0, var1, ... var42 as your folder names, what are you going to do with them? – njzk2 May 05 '17 at 16:55
  • 1
    Suppose you created the variables... how would you know which ones to use later in your program? This looks like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What problem are you trying to solve? – tdelaney May 05 '17 at 16:56
  • Do not create dynamic variables, *use a container like a list or dictionary*. – juanpa.arrivillaga May 05 '17 at 17:14
  • Also, use `dir_list = [f.name for f in os.scandir('aFolder') if f.is_dir()]`, don't do what you are doing with `os.walk`. And `NumbOfList` is just `len(dir_list)` – juanpa.arrivillaga May 05 '17 at 17:14
  • @juanpa.arrivillaga why is there something wrong with `os.walk`? – Sank6 May 05 '17 at 17:15
  • No, but it is meant for walking a directory tree, not getting the second element of the tuple returned by the first iteration. It's a huge waste, and hard to understand what you are trying to do when you read the code. – juanpa.arrivillaga May 05 '17 at 17:16
  • @juanpa.arrivillaga `os.scandir` comes up with an error that module has no attribute scandir. – Sank6 May 05 '17 at 17:18
  • @SankarshMakam is there any reason you are using Python2 instead of Python 3? – juanpa.arrivillaga May 05 '17 at 17:23
  • @juanpa.arrivillaga no, just I can't find my python 3's editing shell/ IDLE. – Sank6 May 05 '17 at 17:43
  • @SankarshMakam you should use Python 3. Python 2 is at it's end of life in 2020. – juanpa.arrivillaga May 05 '17 at 17:44
  • @juanpa.arrivillaga Why what will happen? – Sank6 May 05 '17 at 17:46
  • It will no longer officially be supported. It will be like Windows XP. There are only two good reasons to use Python 2 over 3: your boss is making you, or you have a codebase already written in Python 2. If you are learning to program right now, learn 3, not 2. – juanpa.arrivillaga May 05 '17 at 17:50

2 Answers2

1

If you want to see if a string is a substring of a folder in your list:

for directory in dir_list:
     #Do stuff for everyone
     if "mysubstring" in directory:
         dospecialstuff()
kabanus
  • 24,623
  • 6
  • 41
  • 74
  • Yes, this works... It's just I wanted to see if part of `myname` was in part of any of the list. So as in it would recognise `Hwqdmyname23`. – Sank6 May 05 '17 at 16:54
  • @SankarshMakam I'm not sure I understood, is this the solution? Just write in your code exactly what you're going to do - write the code assuming you have var1, var2 etc.. – kabanus May 05 '17 at 16:56
  • No. I want the `if` statement to recognize folder names with `myname` in it. It doesn't necessarily have to be called `myname`. – Sank6 May 05 '17 at 16:58
  • @SankarshMakam I see now, I'll show a simple (though not optimal solution). – kabanus May 05 '17 at 17:03
  • @SankarshMakam Please use common sense, that's obviously a typo. fixed. – kabanus May 05 '17 at 17:08
  • @SankarshMakam Please next time try to be clear with your questions - for your own benefit. Also, do not forget to accept an answer once you're satisfied - for the benefit of future googlers. – kabanus May 05 '17 at 17:12
1

You asked me to expand my comment. Here it is:

import os

d=dict()
dir_list = os.walk('/tmp').next()[1]

for idx in range(len(dir_list)):
  d.update({dir_list[idx]: idx})

for k, v in d.items():
  print "k=", k, "v=", v
  d.update({k: v+1})  # to update the variable
  print "new value of <%s>"%(k), d[k]
  print
dede
  • 706
  • 9
  • 19