0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonのファイル書き出しのタイミングを自分で調整する

Last updated at Posted at 2024-11-28

目的および背景

実行経過のログをファイルに書き出す処理を行っています。
write()メソッドを実行後、実際にファイルに書かれるまでに一定の時間がかかるため、flush()メソッドを使用して書き出しのタイミングを調整するようにしました。

プログラムコード

write()メソッド実行のタイミングで、ストリームの書き込みバッファをフラッシュするflush()メソッドを実行するようにプログラミングしました。
頻発すると実行速度に影響が出る可能性はありますが、開発途中のデバッグ時などには大変有効でした。

withキーワードを使用したwrite()メソッド
with open("workfile", "w", encoding="utf-8") as f:
   f.write("test")
   f.flush()

参照

以下のページを参考にさせていただきました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?