Dan
Dan

Reputation: 39

For Loop, os.listdir() not working correctly

I am creating a script to create new folder hierarchies for a friend of mine. There are around a thousand clients, so a script would save a ton of time. I have everything almost working, the part I don't have is this.

yearList = os.listdir(driveLetter + clientName)
for year in yearList:
    os.chdir(year)
    os.mkdir('Folder One')
    os.mkdir('Folder Two')
    os.mkdir('Folder Three')

Under this, are creations for sub folders, like so:

# Create folders under 'Folder One'
    os.chdir(driveLetter + clientName + '\\' + year + '\Folder One')
    os.mkdir('Sub Folder One')
    os.mkdir('Sub Folder Two')

2005 is the first element in the list of yearList. This runs fine for 2005, but I get this error:

WindowsError: [Error 2] The system cannot find the file specified: '2006'

This would really help my friend out, so I am pretty motivated to do it (And pretty[read: very] new to programming)

Thanks for any assistance that can be provided

Upvotes: 1

Views: 3265

Answers (4)

tzot
tzot

Reputation: 96081

There's a context manager that you can use to temporarily store the cwd in that question:

How do I write a decorator that restores the cwd?

Upvotes: 0

Joshua Barron
Joshua Barron

Reputation: 1582

I haven't tested this on windows, but os.listdir(...) returns a list containing the names of every item in the given directory (which, in your case, is folders). Keep in mind that this is not an absolute path, so when you switch into the subdirectory you can't just os.chdir() with the next item to get back. You need to either go up a directory, as follows:

os.chdir('..')

or use a different function to keep track of absolute paths.

Also, in this line:

os.chdir(driveLetter + clientName + '\\' + year + '\Folder One')

shouldn't you be escaping the \ in '\Folder One'?

Upvotes: 0

ibz
ibz

Reputation: 46859

After you do a chdir, the current directory changes to the subdirectory, so you can't move to another subdirectory (a sibling of the first one) later using just its name. Using an absolute path will fix your problem.

Try

os.chdir(os.path.join(driveLetter, clientName, year))

Moving back to the parent directory os.chdir("..") before moving to a sibling also works.

As a side note, better use os.path.join(driveLetter, clientName, year, "Folder One") instead of driveLetter + clientName + '\\' + year + '\Folder One'. It will make your code work on other operating systems too. And a bit more readable I would argue.

Upvotes: 0

sth
sth

Reputation: 229934

In the for year in yearList loop you change to the year's subdirectory, but probably never leave it again. So in the first iteration you enter the "2005" subdirectory and in the second iteration you are still in that subdirectory. Then you get the error that there is no "2006" directory (in the current "2005" directory).

You can work around that by leaving the subdirectory again at the end of the loop:

for year in yearList:
    os.chdir(year)
    ...
    os.chdir('..')

Upvotes: 1

Related Questions