


sys.stdout をログ ファイルに複製する
sys.stdout をログ ファイルに複製すると、システム コールを含むすべての出力をキャプチャできます。 、Python アプリケーションから。このタスクは単純であるにもかかわらず、アプリケーションがインタラクティブ モードとデーモン モードの両方で動作する場合に特有の課題を引き起こします。
インタラクティブ モードとデーモン モード
インタラクティブ モードでは、出力を受け取る画面とログ ファイルの両方。ただし、デーモン モードでは、すべての出力がログ ファイルに送られる必要があります。 os.dup2() 関数は、デーモン モードで出力をリダイレクトする場合にはうまく機能しますが、システム コールを変更せずに対話モードで出力を複製するための簡単な解決策は提供しません。
Tee を使用した複製
この問題を解決するには、標準出力をログ ファイルに複製する方法を提供する Tee クラスを使用できます。以下に例を示します。
class Tee(object): def __init__(self, name, mode): self.file = open(name, mode) self.stdout = sys.stdout sys.stdout = self def __del__(self): sys.stdout = self.stdout self.file.close() def write(self, data): self.file.write(data) self.stdout.write(data) def flush(self): self.file.flush()
このクラスを使用すると、ログ ファイル名とモードをパラメータとして受け取る Tee インスタンスを作成できます。 write() メソッドは、出力をログ ファイルと標準出力の両方に複製します。
実装例
Tee クラスを使用するには:
with Tee('logfile.txt', 'w') as tee: print('Hello world!') os.system('ls -la')
この例では、「ls -la」へのシステムコールを含むすべての出力が画面とログの両方に書き込まれます。 file 'logfile.txt'.
このソリューションは、個々のシステム コールを変更する必要がなく、システム コール出力を含むすべての出力を効果的にログに記録します。
以上がインタラクティブ モードとデーモン モードの両方で Python の sys.stdout をログ ファイルに複製するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。

numpyisessentialfornumericalcomputinginpythonduetoitsspeed、memory efficiency、andcomprehensivematicalfunctions.1)それは、performsoperations.2)numpyArraysaremoremory-efficientthanpythonlists.3)Itofderangeofmathematicaloperty

contiguousMemoryAllocationisucial forArraysは、ForeffienceAndfastelementAccess.1)iteenablesConstantTimeAccess、O(1)、DuetodirectAddresscalculation.2)itemprovesefficiencyByAllowingMultiblementFechesperCacheLine.3)itimplifieMememm

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境
