1

I am a newbie in Python, and I only have been programming in the language for the third day. I convert from Matlab because I am graduating from university and I just cannot afford the heavy cost of the software.

I am trying to add several filenames to a list, which fails. The following is my code:

    import os
    dirname = r"D:\MyFiles"
    temp = os.listdir(dirname)
    fl = []
    for fn in temp:
        if fn.startswith('L0116'):
            fl = fl.append(fn)

What I don't understand is, the variable fl when initiated with [] returns as the list type, but then I fail to append the list with the above loop. I recheck my code using interactive prompt and I found out that the type changes to str and fails to append more.

I am stuck at this and I tried to google for about an hour without any clear clue to how I should do this.

Is there a better way? Or which part did I do it wrong?

Thank you so much for your help!

4 Answers 4

2

The append() method mutates the list in place. You should just append fn like this:

fl.append(fn)

don't try to assign the result of fl.append(fn) (which is None, by the way)

Also, don't use r"foo" strings for filenames. They are designed for use with regular expressions and using them for filenames or other kinds of strings will bite you. For example, you cannot end such a string with a backslash.

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

8 Comments

Thanks! Concept-problem after all.
However, if I do not add the r in front, the listdir function will read the the character after '\' and render errors. Is there any way around this?
Use "D:\\MyFiles". Yes, it's ugly to have to double every pathname separator. Considering the backslash has been used as a string escape character for many decades, it was a very poor choice to use it also as a pathname separator. But that's what you're stuck with now if you're stuck with MS Windows.
Thank you again! And yes!! ARGH. The more I code the more I hate MS Windows, though I admit that Win7 looks good.
@Celada: r'' is perfectly acceptable for filenames. It is not "designed for regexps". It is designed as a string that doesn't have escapes.
|
2

append works inplace, so no need to assign it to the list itself (fl = fl.append(fn)) just do fl.append(fn)

Comments

2

List comprehension:

import os
dirname = r"D:\MyFiles"
fl = [fn for fn in os.listdir(dirname) if fn.startswith('L0116')]

Comments

0

List append in python does not return a type, instead, you simply call the method like so:

import os
dirname = r"D:\MyFiles"
temp = os.listdir(dirname)
fl = []
for fn in temp:
    if fn.startswith('L0116'):
        fl.append(fn)

I haven't tested this code, but it should work. Your logic was resetting the list every time.

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.