Python och filhantering - fel på mig eller min dator?

Permalänk
Medlem

Python och filhantering - fel på mig eller min dator?

Sanity check..

olle=open("append.txt", "a+") olles=olle.read() olle.write("test"+"\n") olle.close()

Visst ska detta funka? På min windowsburk med python 2.4 får jag felmeddelandet

Citat:

Traceback (most recent call last):
File "for.py", line 7, in ?
olle.write("test"+"\n")
IOError: (0, 'Error')

På min debianburk med python 3.5 funkar det dock utmärkt att köra ovanstående kodsnutt. Är det fel på mig, min windowsburk, är något borttaget/tillagt i python 2.4 eller håller världen på att gå under?

MVH
JACOB

Permalänk
Medlem

Wow! Det är inte så konstigt att det funkar med Python 3.5 eftersom det kommer från framtiden! Dom har ju inte ens bestämt sig vad som ska finnas i Python 3.0 än, och så sitter du och håller på 3.5:an. Guido blir nog glad om du mejlar honom.

För att få det att funka under Python 2.4 däremot så kanske det hjälper att läsa dokumentationen som säger såhär:

"The first argument is a string containing the filename. The second argument is another string containing a few characters describing the way in which the file will be used. mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end. 'r+' opens the file for both reading and writing. The mode argument is optional; 'r' will be assumed if it's omitted."

Det kanske kan hjälpa.

Visa signatur

Min hemsida: http://www.srekel.net
Pocket Task Force: http://ptf.srekel.net
Kaka e gott! http://kaka.srekel.net