Search code examples
pythonmkdir

Creating folders based on a list read from a text file


I have some code as follows:

from sys import argv
import os;
home_dir = '/home/joga'


script, dirlist = argv
mylist = open(dirlist, 'r')
for folder in mylist:
        newFolder = home_dir+'/'+folder
        print "Folder name " +newFolder
        if not os.path.exists(newFolder):
                os.makedirs(str(newFolder))
        os.chdir(newFolder)
mylist.close()

The idea is to read a list of folders listed in text file, and create each of these folders if they don't already exist. I am getting the folders created, however some have strange names, for example a stray '?' appended to the folder name

How do I fix this?


Solution

  • Answering my own question

    I added a folder = folder.strip() as the first line in my for loop. I guess it line-ending was creating the junk character.