简体   繁体   English

从rc.local开始的python脚本写入文件缺少使用python和linux的第一次写入

[英]Writing to file from a python script started with rc.local is missing the first write using python and linux

Im using a raspberry pi with raspbian, Debain Wheezy Jan 2014 and python3 我使用覆盆子pi与raspbian,Debain Wheezy 2014年1月和python3

I'm starting a python script from rc.local that captures a keyboard input and writes to a file, without logging in. 我正在从rc.local启动一个python脚本,它捕获键盘输入并写入文件,而无需登录。

If the file that the script is writing to has not been created yet, the first keyboard input registers on the screen but isn't written to the file. 如果尚未创建脚本正在写入的文件,则第一个键盘输入将在屏幕上注册,但不会写入该文件。 All subsequent writes work fine. 所有后续写入工作正常。

My code works fine when I run it from the command line as user that's logged in, the first line is written to the new file as expected. 当我从命令行运行它作为登录用户时,我的代码工作正常,第一行按预期写入新文件。

EDITED CODE FROM MIDNIGHTER 来自MIDNIGHTER的编辑代码

#!/usr/bin/env python3.2
import sys
from datetime import datetime

def main():
    f = open('/home/pi/cards.csv','r')
    sim = f.read()
    sim = sim.split('\n')
    simSet = set(sim)

    while True:
        try:
            log = open('logs', 'a')
            puk = input() # text input, i.e., always a string
            included = "true" if puk in simSet else "false"
            print(included, puk)
            log.write("{included: %s, time: %s, number: %s}, \n" % (included, datetime.now(), puk))
            log.close()
        except ValueError:
            log.close()

main()

And the rc.local 和rc.local

 sudo python3 /home/pi/rf1

Im just learning this, please excuse the poor execution. 我刚刚学习这个,请原谅执行不力。

SOLUTION

I realise now I left out an important detail about a cron job closing and copying the file that was being written to. 我意识到现在我遗漏了关于cron作业关闭和复制正在写入的文件的重要细节。

I found my answer here what exactly the python's file.flush() is doing? 我在这里找到了答案python的file.flush()到底在做什么?

Instead of file.close.() I used file.flush() and it works. 而不是file.close.()我使用file.flush() ,它的工作原理。

Code below: 代码如下:

#!/usr/bin/env python3.2
import sys
from datetime import datetime

def main():
    f = open('/home/pi/cards.csv','r')
    sim = f.read()
    sim = sim.split('\n')
    simSet = set(sim)
    log = open('logs', 'a')
    while True:
        try:
            puk = input() # text input, i.e., always a string
            included = "true" if puk in simSet else "false"
            print(included, puk)
            log.write("{included: %s, time: %s, number: %s}, \n" % (included, datetime.now(), puk))
            log.flush()
        except ValueError:
            log.flush()

main()

The problem was I was running a cron job that copied the data to another file which was accessing the file being written to in the python program. 问题是我正在运行一个cron作业,将数据复制到另一个文件,该文件正在访问python程序中正在写入的文件。

The first write after this was not saving to the file as it was being accessed by another program. 之后的第一次写入没有保存到文件,因为它正被另一个程序访问。

These paragraphs seem to be what was happening: 这些段落似乎正在发生的事情:

https://stackoverflow.com/a/7127162/1441620 https://stackoverflow.com/a/7127162/1441620

The first, flush, will simply write out any data that lingers in a program buffer to the actual file. 第一个,flush,将简单地将任何在程序缓冲区中徘徊的数据写出到实际文件中。 Typically this means that the data will be copied from the program buffer to the operating system buffer. 通常,这意味着数据将从程序缓冲区复制到操作系统缓冲区。

Specifically what this means is that if another process has that same file open for reading, it will be able to access the data you just flushed to the file. 具体来说,这意味着如果另一个进程打开同一个文件进行读取,它将能够访问刚冲刷到该文件的数据。 However, it does not necessarily mean it has been "permanently" stored on disk. 但是,它并不一定意味着它已“永久”存储在磁盘上。

I think @Midnighter also suggested using a with statement to open and close the file would have also solved it. 我认为@Midnighter还建议使用with语句来打开和关闭文件也会解决它。

Updated code is in the question > solution. 更新的代码在问题>解决方案中。

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

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