i got this simple code that writes a "abc" into a text file calles test.txt. Now, on a Raspberry execute this code wit IDLE or Thonny does NOT work, the text file remains empty... but instead, when call with the terminal, it writes the text into the file.

f = open("test.txt","w")  f.write("abc")

Is there someone who could explain this?

  • You should provide more details. The name os the programming language would be a start. – RalfFriedl Apr 12 '19 at 5:25
  • python is dynamic binding and very forgiving, so very newbie error prone. In your program segment, you need to bear in mind that the id "f" is the name of a file handle or object. I am writing in newbie style, where I differentiate between filename (a string) and file object, "name" of an object. If you don't differentiate, you will make a big mess when your program gets larger. Also the ending char and new char is also confusing. I intentionally use open file for writing only and read only mode, not the read/write together mode, to avoid carelessly forget this and that errors. – tlfong01 Apr 12 '19 at 5:38
  • Does it explicitly only fail on a Raspberry Pi? If it also fail on other computer then it is a general programming and you should better ask at stackoverflow.com – Ingo Apr 12 '19 at 8:22
  • t works on computer pretty good, only fails on Raspberry – user101360 Apr 15 '19 at 10:12

I'm pretty sure this is a caching problem.

The code

f = open("test.txt","w")

opens the text file for writing and sends the data abc to it, but this data is not immediately written to disk. It remains in a cache in RAM until

  • More data is sent to the file, so the cache gets full and is flushed to disk
  • The code explicitly forces to write the data to disk by f.flush()
  • The code closes the file by f.close()
  • The code reaches the end of the scope where f is valid - for example end of a function or end of the python session

If the two lines of code are just typed into a python terminal like IDLE, the data remains in the cache.

If the two lines of code are stored in a file, and this file is loaded into a python terminal, this is like typing the code - the data remains in cache.

If the two lines of code are stored in a file, and this file is executed by double click, running
python myScriptFile.py
or similar, then python starts, executes the code, and then quits. When python quits, it safely deletes all objects in its memory, which also means it closes all open files. At that moment, the data appears in the file.

| improve this answer | |

Well, there are many reasons that your program doesn't work. Perhaps you can show us the complete program listing.

I have just written a little test program to write to a text file and read it back. You might like to compare with yours to see what goes wrong.

I am using Chinese Windows and so there might be funny characters here and there. In case you cannot read the program, here is a PenZu version:

tlfong01's textFileTest04 - 2019apr12hkt1112

Now the complete listing of the verified program, with sample output. You just plug and play. Good Luck.

# *** write_test_file_test04 tlfong01 2019apr12hkt1101 ***

import datetime

# *** Text file functions ***

# *** Open/Close Text File ***

def openTextFileToWrite(textFileName):
    textFileObject = open(textFileName, 'w')
    return textFileObject

def openTextFileToRead(textFileName):
    textFileObject = open(textFileName, 'r')
    return textFileObject

def closeTextFile(textFileObject):

# *** Write/Read/Print Text File ***

def writeTextString(textFileObject, writeString):

def printTextFile(textFileName):
    textFileObject = open(textFileName, 'r')    
    for line in textFileObject:

# *** Test Functions ***

def writeTimeNowString(textFileObject):
    textFileObject.write('\r\n' + str(datetime.datetime.now()))

def testWriteReadPrintTextFile(textFileName):   
    textFileObject = openTextFileToWrite(textFileName)

# *** Main ***

print('Begin testWriteReadPrintTextFile, ...')
print('\r\nEnd    testWriteReadPrintTextFile.')

# *** Sample Output ***

 RESTART: /home/pi/Python Programs/Python_Programs/test1194/test_text_file_03_2019apr1201.py 
Begin testWriteReadPrintTextFile, ...

2019-04-12 11:00:12.602732

End    testWriteReadPrintTextFile.

# *** End ***
| improve this answer | |
  • Where is the problem solved for a Raspberry Pi? This is better located at stackoverflow.com – Ingo Apr 12 '19 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.