Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Project 2

Hi there, I am currently having trouble in project 2 for the python techdegree. I am trying to figure out how to make a new folder in my treehouse-project-2 folder and I know I'm supposed to use the os.mkdir() statement. I want the new folder to be called "letters", so I can place the 18 text files to each player. First of all, how exactly should I make the directory. Secondly, how can I can I place the 18 text files into that folder? Thanks in advance!

1 Answer

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

Hey Luke Fisher

Since you are a Techdegree student you do have access to the Python Slack channel. You may find questions surrounding specific Techdegree projects more helpful to ask in there for peer students that have built this project.

If your overall goal is to have a letters directory where you can store your .txt files to keep them separated from your .py script files for organization purposes. You could have the Python script create this folder using something like the os.mkdir() function from the os module. Though you may want to have some kind of checking or exception handling in place for this because if the folder exists when this logic runs you would potentially raise FileExistsError.

Another option for you would also be to just manually create that folder as part of your project. Then change the way you open() the file to write each file to append the letters directory to it. The clean way to do this is using the os modules os.path.join() letting Python figure out how to write the path depending on the Operating System it's running on. Instead of hardcoding a Unix or Windows path.

Simple Example:

import os

 os.path.join("folder_name", "filename.txt")
# Windows: 'folder_name\\filename.txt'
# Unix system: 'folder_name/filename.txt'

# So you could do something like this

filepath = os.path.join("folder_name", "filename.txt")
with open(filepath, mode="w"): 
    # ...the rest of your code...

If you are still stuck on this, feel free to reach out in the Slack channel. I provided more context just in case non-Techdegree students had questions about this too. :-)