0

I am getting syntax error in my code. Can anyone say whats wrong in the syntax?

Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 867
            if (file.endsWith(".ear") || file.endsWith(".war")):
                                               ^
SyntaxError: invalid syntax

My code:

for file in [ears for ears in os.listdir(warPath)]:
  if (file.endsWith(".ear") || file.endsWith(".war")):
    file_list.append(file)

I have a warpath where there are multiple war ,ear and sql files. my below condition should read all the files and if condition should filter only war and ear files and store it in a file_list.

1 Answer 1

2

You are using || where you meant to use or:

if file.endswith(".ear") or file.endswith(".war"):

Note that the str.endswidth() method name is all lowercase.

Jython supports at least Python 2.5, so you can just use a tuple:

if file.endswith((".ear", ".war")):

See the Jython string methods documentation:

str.endswith(suffix[, start[, end]])

Return True if the string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for. With optional start, test beginning at that position. With optional end, stop comparing at that position.

Changed in version 2.5: Accept tuples as suffix.

Your for loop looks overly complicated; no need to loop over os.listdir() in a list comprehension:

for file in os.listdir(warPath):
    if file.endswith((".ear", ".war")):
        file_list.append(file)

but if you can use a list comprehension there then you probably are using a Jython 2.7 beta, so you may as well make the file_list definition a list comprehension:

file_list = [f for f ir os.listdir(warPath) if file.endswidth(('.ear', '.war'))]

If you are using an older Jython version (such as 2.1, as bundled with Websphere 8), you cannot use either a tuple argument to str.endswith() nor list comprehensions. You'll have to append still:

for fname in os.listdir(warPath):
    if fname.endswith(".ear") or fname.endswith(".war"):
        file_list.append(fname)

I've used fname here instead of file, which is a built-in name in Python. Best not mask it.

Sign up to request clarification or add additional context in comments.

6 Comments

hi, Thanks for your quick response. I am getting below error when i used if file.endswith((".ear", ".war")): Traceback (innermost last): File "<string>", line 867, in ? TypeError: 1st arg can't be coerced to string
@User050488: looking at the Jython PyString source tuples are definitely supported. What version of Jython is this?
@User050488: Jython 2.5 also supports it, and the error message doesn't match. The 2.7 error message also doesn't match your exception. Not sure what is throwing the exception here, but I cannot see what source line threw that in your traceback.
hi , i am writing this for websphere 8 ,i am not getting what is the supporting jython version for websphere 8
@User050488: see wiki.python.org/jython/WebSphere, you have Jython 2.1. I'll adjust.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.