Home > OS >  I want to make place to collect all path of folder
I want to make place to collect all path of folder

Time:11-16

This is my code.

folder_out = []
for a in range(1,80): 
    folder_letter = "/content/drive/MyDrive/project/Dataset/data/"
    folder_out[a] = os.path.join(folder_letter, str(a))
    folder_out.append(folder_out[a])

and this is an error enter image description here

and this what I want enter image description here

CodePudding user response:

You are using the os method wrong, you want to use os.listdir(Your directory here) to get a list of all directories

import os

dir = os.listdir("/content/drive/MyDrive/project/Dataset/data/")
for f in dir:
    print(f)

If you just want a list of all directories, just use os.listdir("/content/drive/MyDrive/project/Dataset/data/")

CodePudding user response:

It's simply pointless to create a variable. They are unnecessary: You can store everything in lists, dictionarys and so on. Creating a new variables inside loop is very very bad practice.

Code correction

Save in list instead and access them using loops or slicing.

import os
folder_out = []

for a in range(1,80): 
    folder_letter = "/content/drive/MyDrive/project/Dataset/data/"
    folder= os.path.join(folder_letter, str(a))
    folder_out.append(folder)

print(folder_out)

Gives list of folder names.

['/content/drive/MyDrive/project/Dataset/data/1', '/content/drive/MyDrive/project/Dataset/data/2', '/content/drive/MyDrive/project/Dataset/data/3', '/content/drive/MyDrive/project/Dataset/data/4', '/content/drive/MyDrive/project/Dataset/data/5', '/content/drive/MyDrive/project/Dataset/data/6', '/content/drive/MyDrive/project/Dataset/data/7', '/content/drive/MyDrive/project/Dataset/data/8', '/content/drive/MyDrive/project/Dataset/data/9',.....]

If you want to itterate over them.

for elment in folder_out:
    print(elment)

Which gives #

element 1
elem2nt 2...

Like

for x in folder_out:
    print(f"folder_out{c}: {x}")
    c= c 1

Gives what you want

folder_out0: /content/drive/MyDrive/project/Dataset/data/1
folder_out1: /content/drive/MyDrive/project/Dataset/data/2
folder_out2: /content/drive/MyDrive/project/Dataset/data/3
folder_out3: /content/drive/MyDrive/project/Dataset/data/4
folder_out4: /content/drive/MyDrive/project/Dataset/data/5
folder_out5: /content/drive/MyDrive/project/Dataset/data/6
folder_out6: /content/drive/MyDrive/project/Dataset/data/7
folder_out7: /content/drive/MyDrive/project/Dataset/data/8
folder_out8: /content/drive/MyDrive/project/Dataset/data/9
folder_out9: /content/drive/MyDrive/project/Dataset/data/10
folder_out10: /content/drive/MyDrive/project/Dataset/data/11
folder_out11: /content/drive/MyDrive/project/Dataset/data/12
folder_out12: /content/drive/MyDrive/project/Dataset/data/13
folder_out13: /content/drive/MyDrive/project/Dataset/data/14
folder_out14: /content/drive/MyDrive/project/Dataset/data/15
folder_out15: /content/drive/MyDrive/project/Dataset/data/16

If you want to create a folder for each path

import os
for x in folder_out:

    os.mkdir(x)

which will create 79 empty folders

  • Related