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 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?

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