简体   繁体   English

Python脚本在启动时执行时不写入文件

[英]Python script not writing to file when executed at boot

I am trying to make a python loop script that will write to a file. 我正在尝试创建一个将写入文件的python循环脚本。

When I execute the script in terminal, the file gets written with no issue. 当我在终端中执行脚本时,文件写入没有问题。

I wan't this script to start on boot, so I put it in the rc.local file. 我不希望这个脚本在启动时启动,所以我把它放在rc.local文件中。 The script runs, however it is not writing the output to the specified file.. 该脚本运行,但它没有将输出写入指定的文件。

I did some reading on flushing and un-buffered output.. Could anyone help me out 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 write before I even get there.. 当这个脚本完成后,它将使用REST发送文件,但是在我到达那里之前我需要写文件。

The 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()

The /etc/rc.local file: /etc/rc.local文件:

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

loop.py and loop.txt both have read/write/execute access. loop.py和loop.txt都具有读/写/执行访问权限。

Add a full pathname to the statement opening the file: 在打开文件的语句中添加完整路径名:

#!/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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM