hemanta
hemanta

Reputation: 27

Looping over lines in a file and creating multiple directory

I am trying to loop over the lines in a file and create multiple directories. My script is working only for the first line of list in a file. Here is my script. I have attached image of list as well. That is for both list_bottom.dat and list_top.dat.

import os
f = open("list_top.dat", "r")
g = open("list_bottom.dat", "r")
for lines in f:
    m_top = lines.split()[0]

    m_bot = lines.split()[0]
    os.mkdir(m_top)
    os.chdir(m_top)
    for lines in g:
        print(lines)
        m_bot = lines.split()[0]
        print(m_bot)
        os.mkdir(m_top + "_" + m_bot)
        os.chdir(m_top + "_" + m_bot)
        for angle in range(5):
              os.mkdir(m_top + "_" + "m_bot" + "_angle_" + str(angle))
              os.chdir(m_top + "_" + "m_bot" + "_angle_" + str(angle))
              os.chdir("../")
        os.chdir("../")

    os.chdir("../")
os.chdir("../")

enter image description here

Upvotes: 0

Views: 66

Answers (1)

bobrobbob
bobrobbob

Reputation: 1281

you are trying to read from a file pointer, not from its content. you should do this instead

with open("file.txt") as f:
    lines = f.readlines()

for line in lines:
    do_stuff()

(for readability i don't post this as a comment, but that's a comment)

Upvotes: 2

Related Questions