Using endswith to read list of files doesn't find extension in list

前端 未结 3 724
我在风中等你
我在风中等你 2021-01-21 09:11

I am trying to get my python script to read a text file with a list of file names with extensions and print out when it finds a particular extension (.txt files to be exact). It

3条回答
  •  执笔经年
    2021-01-21 09:48

    Each line ends with a new line character '\n' so the test will rightly fail. So you should strip the line first then test:

    line.rstrip().endswith('.txt')
    #      ^
    

提交回复
热议问题