edelwater
edelwater

Reputation: 2802

Why do I get: Invalid Syntax

I am getting invalid syntax on the following:

rootdir = 'c://temp/test//files//'
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        fileParts = file.split('.')
        if len(fileParts) > 1:
            stripper = fileParts([len(fileParts)-2]
            print(stripper)

Upvotes: 0

Views: 406

Answers (2)

neildd
neildd

Reputation: 39

The error was in line 6 of code, correct version below:

stripper = fileParts([len(fileParts)-2]

You need a syntax check editor or IDE.

Upvotes: 0

Levon
Levon

Reputation: 143162

stripper = fileParts([len(fileParts)-2]
                                       ^

there's a missing closing parenthesis ), should be:

stripper = fileParts([len(fileParts)-2])
                                       ^

Aside: You might find some of the functions in the os.path module useful, in particular os.path.split() and os.path.splitext(). Should you need to put paths together later, os.path.join() is good to use.

Upvotes: 6

Related Questions