How to create a folder for each item in a directory?

Posted by Adrian Andronic on Stack Overflow See other posts from Stack Overflow or by Adrian Andronic
Published on 2012-07-11T03:10:06Z Indexed on 2012/07/11 3:15 UTC
Read the original article Hit count: 499

Filed under:
|

I'm having trouble making folders that I create go where I want them to go. For each file in a given folder, I want to create a new folder, then put that file in the new folder. My problem is that the new folders I create are being put in the parent directory, not the one I want. My example:

def createFolder():
dir_name = 'C:\\Users\\Adrian\\Entertainment\\Coding\\Test Folder'
files = os.listdir(dir_name)
for i in files:
    os.mkdir(i)

Let's say that my files in that directory are Hello.txt and Goodbye.txt. When I run the script, it makes new folders for these files, but puts them one level above, in 'C:\Users\Adrian\Entertainment\Coding.

How do I make it so they are created in the same place as the files, AKA 'C:\Users\Adrian\Entertainment\Coding\Test Folder'?

© Stack Overflow or respective owner

Related posts about python

Related posts about python-3.x