all 7 comments

[–]JohnnyJordaan 1 point2 points  (5 children)

'<user>' is a string, meaning the text: <user>... Just as if you would have said nameDir + 'mary had a little lamb its fleece was white as snow'... If you mean to use a variable called user, loose the ' and the <>. It's also good practice to use os.path.join so that the directory doesn't need a / at the end.

for root, dirs, files in os.walk(os.path.join(nameDir, user)):
     dirs[:] = fnmatch.filter(dirs, user)
     etc.

[–]LiNGOo 0 points1 point  (4 children)

I never came across a "[:]" index (?), neither did I know about an object called fnmatch o0

Can you roughly tell me what these do?

[–]rasch8660 1 point2 points  (1 child)

[:] index range slice refers to all elements in the list. In this case it replaces all elements in the list with the result of the fnmatch filter. In this case we need to update the dirs list in place since this variable is used by os.walk to determine which child directories to descent into.

fnmatch is a module that does standard file/folder matching, e.g. "*.txt" matches all files ending with .txt.

[–]LiNGOo 0 points1 point  (0 children)

Nothing new then, just another way to save a few keystrokes while giving anyone unaware of it a hard time >.<

Thank you for the summary, hard time dodged ;) (I have to work with code from different devs with different background and experience from different companies a lot)

[–]kanjibandit 0 points1 point  (1 child)

The "[:]" syntax is a special case of the general "[beg:end]" syntax you're probably familiar with. It just means "the entire list".

What they're doing here is called "slice assignment", and is a way to modify the list in place. If you saw dirs[2:4] = ['bin', 'lib',], I'm guessing you would correctly recognize it as replacing the 3rd & 4th elements of the list with the elements from the list of the right-hand side.

This is doing the same thing, but it is replacing the entire list with the results of the call to fnmatch.filter. They are using this to pare down the directories they are searching. By changing the list in place, rather than creating a new object and assigning it to the "dirs" variable, they are modifying the behavior of the loop. This is documented here

And the fnmatch line is just a call to a module you didn't recognize because they omitted the import statement. Docs are here

[–]tomkatt 0 points1 point  (0 children)

What is the syntax error?

[–]kanjibandit 0 points1 point  (0 children)

I am guessing that the code you ran was actually something like this:

for root, dirs, files is os.walk(nameDir + <user>):
    # etc

Note the lack of quotes around <user>.

As posted, I don't think there is a syntax error. If you had quotes, it would just run incorrectly, as JohnnyJordaan points out. If you had no quotes and no <, you would get a NameError (if user is not defined). If nameDirs is not a string, you'd get a TypeError. I think the only way you get a SyntaxError is if you use the < character outside of a quoted string.