將控制台輸出重新導向到檔案
在Python 中,將stdout 串流重新導向至檔案允許開發人員擷取並儲存列印輸出。為了實現這一點,可以操作 sys.stdout 屬性。但是,最近報告了一個問題,原始問題中概述的方法無法產生所需的輸出。
問題解決
問題在於不正確的列印時使用 file= 參數。在 Python 3.x 及更高版本中,正確的語法是 print('Filename:', filename, file=f)。對於 Python 2.x,使用 print >> f, '檔名:', 檔名。
或者,可以使用 with 語句更簡潔地重新導向 stdout:
with open('output.txt', 'w') as f: with redirect_stdout(f): print('Filename:', filename)
其他方法
除了使用sys.stdout之外,還有其他方法可以重新導向輸出:
-
使用檔案對象:直接寫入檔案對象,如下所示:
with open('out.txt', 'w') as f: print('Filename:', filename, file=f)
-
外部Shell重定向: 使用> shell 中的運算子用於重定向輸出:
./script.py > out.txt
故障排除
在提供提供。的腳本中,確保:
- 指定的路徑(/home/xxx/nearline/bamfiles)包含有效的.bam檔案
- glob.glob 函數正確辨識這些檔案
- samtools 執行檔的完整路徑是正確的(/share/bin/samtools/samtools)
改善結果
使用更正的語法和文件對象,預期輸出將是:
Filename: ERR001268.bam Readlines finished! Mean: 233 SD: 10 Interval is: (213, 252)
此更新版本的腳本捕獲輸出並將其重定向到output .txt 檔案。
以上是如何將控制台輸出重新導向到 Python 中的檔案?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

toAppendElementStoApythonList,usetheappend()方法forsingleements,Extend()formultiplelements,andinsert()forspecificpositions.1)useeAppend()foraddingoneOnelementAttheend.2)useextendTheEnd.2)useextendexendExendEnd(

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

金融、科研、医疗和AI等领域中,高效存储和处理数值数据至关重要。1)在金融中,使用内存映射文件和NumPy库可显著提升数据处理速度。2)科研领域,HDF5文件优化数据存储和检索。3)医疗中,数据库优化技术如索引和分区提高数据查询性能。4)AI中,数据分片和分布式训练加速模型训练。通过选择适当的工具和技术,并权衡存储与处理速度之间的trade-off,可以显著提升系统性能和可扩展性。

pythonarraysarecreatedusiseThearrayModule,notbuilt-Inlikelists.1)importThearrayModule.2)指定tefifythetypecode,例如,'i'forineizewithvalues.arreaysofferbettermemoremorefferbettermemoryfforhomogeNogeNogeNogeNogeNogeNogeNATATABUTESFELLESSFRESSIFERSTEMIFICETISTHANANLISTS。

除了shebang線,還有多種方法可以指定Python解釋器:1.直接使用命令行中的python命令;2.使用批處理文件或shell腳本;3.使用構建工具如Make或CMake;4.使用任務運行器如Invoke。每個方法都有其優缺點,選擇適合項目需求的方法很重要。

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器