Reaper Reaper - 7 months ago 17
Python Question

Python: rstrip() does not work on Linux, but on Windows

this is my first question so sorry if its stupid or obvious wy my code isnt working.

So i wrote some code on Windows with Python and everything worked fine. Then i wanted to port my Code to Linux. After some problems it worked but not everything.

The Problem is:
I use rstrip() while reading stuff from a text file and writting it into a list.
On Windows everything works fine, but on Linux the code doesnt work.. I dont know why and a i cant get it to work.

When i try to execute the code on Linux i get the following error:


AttributeError: 'list' object has no attribute 'rsplit'


I also tried split()..

Here is a part of my code which is working on windows:

f = open('Quiz.txt','r')
for line in f:
line = Wissen
new_nummer = int(f.readline())
new_kat = f.readline()
new_frage = f.readline().rstrip()
new_a1 = f.readline().rstrip()
new_a2 = f.readline().rstrip()
new_a3 = f.readline().rstrip()
new_a4 = f.readline().rstrip()
new_loesung = f.readline().rstrip()
WissenF.append(Wissen(new_nummer,new_kat,new_frage,new_a1,new_a2,new_a3,new_a4,new_loesung))


I´m using Python 3.5.1+ on Ubuntu 16. And Python 3.5.1+ on Windows 8.1.

I´m new so sorry for the bad post "design" if you need further code or information just tell me and i will help out.

Thanks in advance

Answer

The snippet of the traceback that you posted

AttributeError: 'list' object has no attribute 'rsplit'

Indicates that you're trying to call rsplit on a list. f.readline() doesn't produce a list on any system, and the most obvious way to "accidentally" get a list is if you typo'd f.readlines() instead (which gives a list of all the lines in the file-like object f)

Change the line that reads

f.readlines().rstrip()

to

f.readline().rstrip()
Comments