Monday, December 5, 2011

How to make a directory in Python (Aka a Folder)

Hola!

I originally wasn't gonna do a post, but i figured it couldn't hurt. Though, as a heads up this particular lesson has nothing to do with animation. I learned two commands this week:

os.mkdir()
os.mkdir(os.path.join())

The first command makes a directory. The second puts directory B into directory A

Here's a short script that would make those two commands functional.

import os
#Makes it so that python can talk to the operating system
import os.path
#Makes it so that this script is user friendly in both Mac OSX and Windows

os.mkdir('Folder A')
os.mkdir(os.path.join('Folder A', 'Folder B'))

Now, this script will maker Folder A, and put Folder B inside of Folder A. I made this script to try and streamline a file creating system at work vs people copying and pasting folders all day.

How to Run when you're done:
1) Open the command prompt (Windows) or Terminal (Mac OSX)
a. To find the command prompt, just go to start and type in "command prompt" in that seach box (Windows)
b. Go to Finder > Applications > Utilities > Terminal (Mac OSX)

2) Change the directory to wherever you want your set of folders to be created
a. just type in:
cd [Name of Directory without brackets]

ex. cd Desktop
p.s. to go back a directory just simple type, cd..

3. Type in the path of where your python script lives
a. don't know the path? Right click and click on Get Info for mac users and Properties for Windows users
b. Paste the path into the terminal
c. Hit enter

Now if you didn't run into any errors you should have your folders sitting on the Desktop! If you did run into errors check to see why the computer is yelling at you, and try your best to fix it. Otherwise google the problem, or if you trust me email me at: tleong127@gmail.com

As I keep learning python I plan to add more features to this script, so stay tuned!

Thanks for reading, and have a nice day :)

No comments:

Post a Comment