在Linux中达到一定大小后,停止将Python脚本写入文件

在Linux中达到一定大小后,停止将Python脚本写入文件

问题描述:

Python和Linux有所新.我创建了一个脚本,用于挖掘Twitter的流API.当流中的内容与我的参数匹配时,脚本将写入.csv文件.

Somewhat new to Python and new to linux. I created a script that mines Twitter's streaming API. Script writes to a .csv file when things in the stream match my parameters.

我想知道一旦文件达到1 gig,是否有任何方法可以停止我的脚本.我知道cron可以用来计时脚本和所有内容,但是我更关心文件的大小而不是时间.

I'd like to know if there's any way to stop my script once the file has reached 1 gig. I know cron can be used to time the script and everything, but I'm more concerned about the file size than the time it takes.

感谢您的投入和考虑.

在您的情况下,您可能不需要 os.stat os.stat 可能会给出在某些情况下,您的大小是错误的(即缓冲区不刷新).为什么不只使用 f.tell()来读取类似这样的大小

In your case, you probably don't need os.stat and os.stat may give you a false size in some cases (namely buffers not flushing). Why not just use f.tell() to read the size with something like this

with open('out.txt', 'w', encoding='utf-8') as f:
    csvfile = csv.writer(f)
    maxsize = 1024                # max file size in bytes
    for row in data():
        csvfile.writerow(row)
        if f.tell() > maxsize:    # f.tell() gives byte offset, no need to worry about multiwide chars
            break