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.

21
  • 1
    Aren't you already storing them, in dir_list? Commented May 5, 2017 at 16:46
  • 3
    Why is the list not sufficient? Why do you need to create separate variables? This is just a bad idea from the start. Commented May 5, 2017 at 16:47
  • 1
    Than write that in your question. Write exactly what you need afterwards. This is an xy. Commented May 5, 2017 at 16:50
  • 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}) Commented May 5, 2017 at 16:52
  • 1
    Suppose you created the variables... how would you know which ones to use later in your program? This looks like an XY Problem. What problem are you trying to solve? Commented May 5, 2017 at 16:56

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

I must be missing something right in front of me, but what is k and v?
k and v are "key" and "value" of the dictionary. Each key has its value. The key is your variable name and value is its individual value.
Is the value random?
sure not :-) you have to set it. It can be anything.
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()

6 Comments

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.
@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..
No. I want the if statement to recognize folder names with myname in it. It doesn't necessarily have to be called myname.
@SankarshMakam I see now, I'll show a simple (though not optimal solution).
@SankarshMakam Please use common sense, that's obviously a typo. fixed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.