修改代码以在新创建的文件夹中创建文件

时间:2019-04-20 19:14:41

标签: python

我正在https://raspberrypi.stackexchange.com/questions/26714/can-i-record-a-24-hour-video-on-the-raspberry-pi-with-camera-module中尝试修改代码:

import io
import itertools
import picamera

def outputs():
    for i in itertools.count(1):
        yield io.open('file%02d.h264' % i, 'wb')

with picamera.PiCamera() as camera:
    camera.resolution = (640, 360)
    camera.framerate = 24
    for output in camera.record_sequence(
            outputs(), quality=20, bitrate=750000):
        while output.tell() < 1048576:
            camera.wait_recording(0.1)
        if output.name == 'file99.h264':
            break

此代码将“每产生一个大于1Mb的文件就会产生100个文件,”

我正在尝试进行修改,以便一旦文件达到> 5Mb,就会创建一个新文件。应该创建范围从1到100的目录,每个目录最多包含100个文件。到目前为止,这是我的进度:

import io
import itertools
import picamera
import os

def outputs():
    for i in itertools.count(1):
        yield io.open('file%02d.h264' % i, 'wb')

for i in range(1 , 101) :

    os.mkdir(str(i))
    with picamera.PiCamera() as camera:
        camera.resolution = (640,480)
        camera.framerate = 24
        for output in camera.record_sequence(
                outputs(), quality=20, bitrate=750000):
            while output.tell() < 5048576:
                camera.wait_recording(0.1)
            if output.name == 'file99.h264':

如何在新创建的文件夹中创建文件并检查此新创建的文件夹中是否包含100个文件?

1 个答案:

答案 0 :(得分:0)

您可以使用第一段代码并修改其output函数:

def outputs():
    dir_count = 0
    current_dir = 'dir{}'

    for i in itertools.count(1):
        if not (i % 100):
            dir_count += 1
            os.mkdir(current_dir.format(dir_count))
        yield io.open((current_dir + '/file{:02d}.h264').format(current_dir, i % 100), 'wb')

或更短:

def outputs():
    yield from (
        io.open('dir{}/file{:02d}.h264'.format(dir, i), 'wb')
        for dir in itertools.count(1)
        for i in range(100)
        if os.makedirs('dir{}'.format(dir), exist_ok=True) is None
    )

每个目录的文件数为10时,将创建以下文件:

$ ls -h ./*
./dir1:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir10:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir11:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir12:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir13:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir14:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir15:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir16:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir17:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir18:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir19:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir2:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir20:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

./dir21:
file00.h264 file02.h264 file04.h264 file06.h264 file08.h264
file01.h264 file03.h264 file05.h264 file07.h264 file09.h264

# the list goes on and on...

警告!不要忘记关闭打开的文件,因为其中会有很多 ,您会收到错误消息如果不关闭文件,则同时打开的文件太多。