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 trialHanwen Zhang
20,084 PointsWhat is wrong with my code? why it shows the name is not defined?
from distutils.util import strtobool
import os
import pathlib
import re
import sys
import unicodedata
DIRS = [
'{project_slug}/',
'{project_slug}/static',
'{project_slug}/static/img',
'{project_slug}/static/js',
'{project_slug}/static/css',
'{project_slug}/templates',
]
def slugify(string):
string = unicodedata.normalze("NFKC", string)
string = re.sub(r'[^\w\s]', '', string).string().lower()
return re.sub(r'[_-\s]+', '_', string)
def get_root():
root = pathlib.PurePath(
input("What is the full path where you would like the project?")
)
if not root.is_absolute():
return get_root()
return root
def check_delete_rot(root):
if os.path.exists(root):
prrint("Path already exists.")
try:
delete = strtobool(input("Delete existing files/directories? [y/n] ").lower())
except ValueError:
return check_delete_root(root)
else:
if delete:
try:
os.removedirs(root)
except OSError:
print("Couldn't remove {}. Please delete it yourself.".format(root))
else:
print("Delete {}".format(root))
def crete_dirs(root, slug):
try:
os.markedirs(root)
except OSError:
print("Could not create the project root at {}.".format(root))
sys.exit()
else:
for dir in DIRS:
try:
os.mkdir(os.path.join(root, dir.format(project_slug=slug)))
except FileExistsError:
pass
def main():
project_root = get_root()
check_delete_root(project_root)
project_name = None
while not project_name:
project_name = input("what is the full name for the project?").strip()
project_slug = slugify(project_name)
create_dirs(project_root, project_slug)
print("creating {} in {}".format(project_name, project_root))
if __name__ == '__main__':
main()
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsPlease include the stacktrace information showing the error you are seeing.