0

i have a .csv file such as the following:

name1,name2,name3 and so on

using Python script i am trying to have it read the .csv and make directories for each value eg: name1,name2,name3 will create these directories :name1 and name2 and name3

this is my code so far:

import os
import fileinput
textFile = 'E:/Videos/Movies/subtest/dirlist.csv'
path = "E:/Videos/Movies/subtest/"

#generate a txt file with the current names of the directories
def makeFile():
    # Open a file
    dirs = os.listdir( path )
    # This would print all the files and directories
    for file in dirs:
        #open the file
        tFO = open(textFile, "ab+")
        #write to the file, seprating each item with "||"
        tFO.write( file + ',' ) 
        #print output
        print ( file )
        #prints confirmation
        print 'file printed!'
        #close the file
        tFO.close()
    mainMenu()

def makeDirs():
    #open textFile as read only and set its varible as myListRead
    myListRead = open(textFile, 'rb+')
    #reads the x amount of lines and stores it as str
    str = myListRead.read();
    for line in str:
        os.makedirs(path + str)
    print 'directories created:', str

running this code creates the .csv as i intended, but when i run makeDirs() it makes the directory name all of the .csv (name1,name2,name3 as the foldername)

4
  • 2
    Can you clarify what is wrong with your code? Commented Jul 31, 2013 at 16:30
  • 1
    What exactly is your question? Commented Jul 31, 2013 at 16:30
  • Can you print out what path + str is from your os.makedirs and post it here? Commented Jul 31, 2013 at 16:39
  • @sihrc path + str yields: E:/Videos/Movies/subtest/dirlist.txt,run.py Commented Jul 31, 2013 at 16:56

1 Answer 1

2

Your problems become immediately obvious if you add some print statements to your code.

Given an input file that looks like:

name1,name2,name3

The following code:

str = myListRead.read();
for line in str:
    print 'LINE:', line

Would print:

LINE: n
LINE: a
LINE: m
LINE: e
LINE: 1
LINE: ,
LINE: n
LINE: a
LINE: m
LINE: e
LINE: 2
LINE: ,
LINE: n
LINE: a
LINE: m
LINE: e
LINE: 3
LINE: 

That is, you're iterating over characters, not comma delimited items. The read() method reads in the entire file as a single string. You get a sequence of characters, not a sequence of lines.

If you want to iterate over lines in a file, you don't need to call read(), you can just to this:

myListRead = open(textFile, 'rb+')
for line in myListRead:
    print 'LINE:', line

Which would yield:

LINE: name1,name2,name3

Of course, you're going to need to split this line on commas. You could do this:

for line in myListRead:
    for item in line.strip().split(','):
        os.makedirs(os.path.join(path, item))
        print 'created', item

You could also think about using the built-in csv module for parsing your CSV file, although that may be overkill for your particular use case.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! The last part of code there worked for me, I also had to create the directories to a new file path as i can't create 2 of the same directory.

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.