Python script not writing to file on execution at boot

I am trying to create a python script loop that will write to a file.

When I execute the script in the terminal, the file is written without problems.

I do not want this script to run at boot, so I put it in the rc.local file. The script is executed, however, it does not write the output to the specified file.

I read a little about flushing and non-buffering. Can someone help me or point me in the right direction?

When this script is finished, it will send the file using REST, but I need the file to be written before I even get there.

script:

#!/usr/bin/python -u

while True:
    try:
        print "This is only a test..."
        with open("loop.txt", "a") as loopFile:
            loopFile.write("This is only a test...")
            loopFile.write('\n')
            loopFile.flush()
            loopFile.close()
        time.sleep(1)
    except KeyboardInterrupt:
        break
        quit()

File / etc / rc.local:

/usr/bin/python /home/pi/loop.py &

loop.py and loop.txt have read / write / execute access.

+4
source share
1

, :

#!/usr/bin/python -u

while True:
    try:
        print "This is only a test..."
        with open("/home/users/sj091190/loop.txt", "a") as loopFile:
            loopFile.write("This is only a test...")
            loopFile.write('\n')
            loopFile.flush()
        time.sleep(1)
    except KeyboardInterrupt:
        break
        quit()
+5

Source: https://habr.com/ru/post/1628868/


All Articles