python if and same instruction line not working

MRAB python at mrabarnett.plus.com
Sun Oct 4 12:39:32 EDT 2020


On 2020-10-04 10:35, pascal z via Python-list wrote:
> On Tuesday, September 29, 2020 at 5:28:22 PM UTC+2, MRAB wrote:
>> On 2020-09-29 15:42, pascal z via Python-list wrote:
>> > I need to change the script commented out to the one not commented out. Why?
>> > 
>> >      # for x in sorted (fr, key=str.lower):
>> >      #     tmpstr = x.rpartition(';')[2]
>> >      #     if x != csv_contents and tmpstr == "folder\n":
>> >      #         csv_contentsB += x
>> >      #     elif x != csv_contents and tmpstr == "files\n":
>> >      #         csv_contentsC += x
>> > 
>> >      for x in sorted (fr, key=str.lower):
>> >          if x != csv_contents:
>> >              tmpstr = x.rpartition(';')[2]
>> >              if tmpstr == "folder\n":
>> >                  csv_contentsB += x
>> >              elif tmpstr == "file\n":
>> >                  csv_contentsC += x
>> > 
>> You haven't defined what you mean by "not working" for any test values 
>> to try, but I notice that the commented code has "files\n" whereas the 
>> uncommented code has "file\n".
> 
> Very good point, it should what caused the issue
> 
> By the way, it seems it's ok to check \n as end of line, it will work on windows linux and mac platforms even if windows use \r\n
> 
By default, when the 'open' function opens a file in text mode, it uses 
"universal newlines mode", so the rest of the program doesn't have to 
worry about the differences in line endings. It's explained in the 
documentation about the 'open' function.


More information about the Python-list mailing list