0

I am trying to place all files within a directory into it's own individual folder. When trying to declare the variable 'folders' I enumerate the list of files and try to append the enumerated list number to the end of the folder name, so the folders would look something like... FOLDER_1, FOLDER_2, FOLDER_3, where the trailing digit is generated from enumeration of the files in the directory.

However my code below lists them all as 'FOLDER_0' and then hits an overwrite error, my increment doesn't seem to be working, any direction into what I am doing wrong would be much appreciated.

PATH = "C:/Temp/"

def main(): 
    files = [ join(PATH, f) for f in listdir(PATH) if isfile(join(PATH, f)) ]

    for i, f in enumerate (files):
        folders = [ PATH+"FOLDER_"+str(i)+"/" for f in files ]
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
MikG
  • 1,019
  • 1
  • 15
  • 34
  • 1
    What's the exact error? – vaultah Jun 16 '14 at 17:19
  • What does `files` look like after your list comprehension? – Elias Jun 16 '14 at 17:20
  • What would you like to have in `folders` after the for-loop? Now you iterate through files but use `f` nowhere, i.e. the last line is equivalent to folders = [ PATH+"FOLDER_"+str(i)+"/" ] * len(files), which may not be what you want. Also, you are using the variable `f` twice (outer and inner loop), which really may cause problems. – DrV Jun 16 '14 at 18:06
  • Each time you perform the `for` cycle, a new variable `folders` is created. You must initialize an empty variable `folders = []` before the `for` cycle and then use the `list.append()` method or the `+=` operator to add a new item: `folders.append([ PATH+"FOLDER_"+str(i)+"/" for f in files ])` – s3n0 Mar 05 '19 at 13:37
  • However, I see more discrepancies. Why do you use `enumerate` if you do not need it? You can also use: `for i in range(len(files))` or make a one-line function as for the `files` variable: `folders = [ PATH+"FOLDER_"+str(i)+"/" for i in range(len(files)) ]` – s3n0 Mar 05 '19 at 13:53

1 Answers1

1

It is not really clear what your end goal is, but from what you have shown in your code and written in your question I think I can take a stab at it.

  • To start python uses zero based indexing, so the first value corresponding to the first item in enumerate will be 0.

  • Next your for loop is equivalent to doing something like this:

      folders = [PATH + "FOLDER_" + str(len(files))] * len(files)

      So for...

      files=["file1", "file2"]

      you would get...

      folders=["FOLDER_1", "FOLDER_1"]

  • This is likely where the "overwrite error" is indirectly coming form. If you tried to create every folder in folders you get an OSError telling you that you are creating a folder you just created.

If you wanted to create a folder for each file you had in a directory, I would try to modify your code into this direction:

PATH = "C:/Temp/"

def main():
    files = [join(PATH, f) for f in listdir(PATH) if isfile(join(PATH, f))]
    folders = [join(PATH, "FOLDER_{}".format(i + 1)) for i, _ in enumerate(files)]
    ...

And then if you are going to create all the folders in folders check they don't already exist

Community
  • 1
  • 1
MrAlias
  • 1,316
  • 15
  • 26
  • Thank you so much MrAlias, your solution is exactly what I was looking for, and much more efficient than my original method. Cheers! – MikG Jun 16 '14 at 19:56