Python コードの出力リダイレクト
クエリは、Python を使用して print ステートメントを指定されたテキスト ファイルにリダイレクトすることに重点を置いています。 sys.stdout 手法を使用しているにもかかわらず、ターゲット ファイルは空のままです。この記事は、問題を調査し、代替解決策を提供することを目的としています。
出力にファイル オブジェクトを利用する
最も簡単なアプローチには、ファイル オブジェクトに直接書き込む方法があります。
with open('out.txt', 'w') as f: print('Filename:', filename, file=f) # Python 3.x # print >> f, 'Filename:', filename # Python 2.x
リダイレクト標準出力
または、次のコードを使用して stdout リダイレクトを実現できます。
import sys orig_stdout = sys.stdout f = open('out.txt', 'w') sys.stdout = f for i in range(2): print('i = ', i) sys.stdout = orig_stdout f.close()
Standard Library Context Manager の使用
Python の場合3.4 以降では、専用のコンテキスト マネージャーを使用してこれを簡素化できます。プロセス:
from contextlib import redirect_stdout with open('out.txt', 'w') as f: with redirect_stdout(f): print('data')
外部出力リダイレクト
シェル経由で出力を外部にリダイレクトするもう 1 つの実行可能なオプション:
./script.py > out.txt
追加考慮事項
- 確認glob.glob.
- を使用して、指定されたパス内の bamfile の可用性を確認します。パスとファイル名を効率的に操作するには、os.path.join と os.path.basename を利用します。
以上がPython の印刷出力をファイルにリダイレクトするにはどうすればよいですか?また、失敗する可能性があるのはなぜですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

forloopsareadvastountousforknowterations and sequences、offeringsimplicityandeadability;

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

pythonisnotpurelyLepted; itusesahybridapproachofbytecodecodecodecodecodecodedruntimerttation.1)pythoncompilessourcodeintobytecode、whodythepythonvirtualmachine(pvm).2)

ToconcatenateListsinpythothesheElements、使用:1)Operatortokeepduplicates、2)asettoremoveduplicates、or3)listcomplunting for controloverduplicates、各メトドハスディフェルフェルフェントパフォーマンスアンドソーダーインプリテーション。

pythonisantertedlanguage、useaseofuseandflexibility-butfactingporformantationationsincriticalapplications.1)解釈されたlikepythonexecuteline-by-lineを解釈します


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

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

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

Dreamweaver Mac版
ビジュアル Web 開発ツール

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境
