I have a list containing strings.
I want to make a directory for each item in the list, with the name of the directory being the string.

Does anyone know if this can be done, and it so, how?

Recommended Answers

All 2 Replies

I assume your internet connection is broken, so you can not use Google and your IDLE help file is deleted, so you can not use IDLE's help menu. :P

So, jokes aside, this kind of OS spesific, but generally available function are in os module.

So IDLE interactive use:

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************
    
IDLE 2.6.5      
>>> import os
>>> print os.listdir(os.curdir)
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'numpy-wininst.log', 'psyco-wininst.log', 'python.exe', 'pythonw.exe', 'README.txt', 'Removenumpy.exe', 'Removepsyco.exe', 'Removescipy.exe', 'scipy-wininst.log', 'Scripts', 'tcl', 'Tools', 'unicows.dll', 'w9xpopen.exe']
>>> list_of_dirs=['a','b','c']
>>> for dirname in list_of_dirs:
	try:
		os.mkdir(dirname)
	except:
		print 'Failure in creating',dirname

		
>>> print os.listdir(os.curdir)
['a', 'b', 'c', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'numpy-wininst.log', 'psyco-wininst.log', 'python.exe', 'pythonw.exe', 'README.txt', 'Removenumpy.exe', 'Removepsyco.exe', 'Removescipy.exe', 'scipy-wininst.log', 'Scripts', 'tcl', 'Tools', 'unicows.dll', 'w9xpopen.exe']
>>>

just use mkdir function in os module
eg:
your list , l=
for i in l:
os.mkdir(i)

thats all for creating a list of folders in the current working directory
Look at the OS module docs for more functionalities

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.