1

When running the code below, I got the error message saying 'TypeError: expected str, bytes or os.PathLike object, not list'

I used Spyder to debug the above error message - but I could not find the bug. Could someone please tell me how to solve this error? The code is given below

Thank you!

J1 = ['520A','520B']
J2 = ['560S','580A']

JOINTS = J1 + J2

FILES_IN = ['dynres-x-eqk.lst','dynres-y-eqk.lst','dynres-z-eqk.lst'] 
FILE_OUT = ['_Accelerations.txt']

def joint_acc(joint, file_in):
    output = []
    with open(file_in, 'rt') as f_in:
        for line in f_in:
            if 'JOINT ACCELERATIONS' in line:
                for line in f_in:
                    if line.startswith(joint):
                        while line.strip() != '':
                            output.append(line)
                            line = next(f_in)
                        break
    return ''.join(output)

with open(FILE_OUT, 'wt') as f_out:
    for Jt in JOINTS:
        for filename in FILES_IN:
            f_out.write(joint_acc(Jt, filename))
        f_out.write('\n')

1
  • 1
    Welcome to Stack Overflow. Don't forget to accept an answer (tick the check-mark next to an answer) if it answers your question. In this way your question stops to show up as unanswered. Also up-vote good answers. Note that accepting and up-voting answers is the way to say thanks on Stack Overflow. – As you're starting out here, please take the tour, read about what's on-topic, and have a look at How to Ask a Good Question. Commented Jul 30, 2020 at 13:14

2 Answers 2

6

You did:

FILE_OUT = ['_Accelerations.txt']

then:

with open(FILE_OUT, 'wt') as f_out:

So this open get list, which cause TypeError. Try replacing FILE_OUT = ['_Accelerations.txt'] with FILE_OUT = '_Accelerations.txt'

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

Comments

0

Your FILE_OUT variable is list type but the open waits for str, bytes or os.PathLike object types.

You should convert your FILE_OUT variable to a correct type.

For example to string:

FILE_OUT = "_Accelerations.txt"

Or your can access the first element of your list with 0 index (Your list has only 1 element.)

Like this:

with open(FILE_OUT[0], 'wt') as f_out:

Comments

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.