Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python for File Systems Project Starter Feeling Sluggish

Stephen Hutchinson
Stephen Hutchinson
8,426 Points

Why is the slugify Challenge giving Bummer! Try again!?

So I am just prepending a _ to the beginning of a file name. when an existing file exists.

I am allowing funny file names, like those that contain a !, is that a problem?

slug.py
import re
import os

def slugify(string):
    # slugs should be unique
    # filename and directory names must be unique

    # slugs should not have spaces in them
    string = re.sub(r'[_\-\s]', '_', string)

    # should start with a number, letter or underscore
    if not re.match(r'^[_\d\w]{1}[^\s]*', string):
        string = '_' + string

    for file in os.scandir(os.getcwd()):
        if file.name == string:
            string = '_' + string

    return string

1 Answer