[英]How can I alter the universal line ending behavior globally?
我想在 Windows 上寫出所有以 LF 結尾而不是 CRLF 的文本文件。 不僅適用於我的代碼,還適用於所有其他第三方依賴項。
我知道我可以為我的代碼open(..., newline='\n', ...)
。 但是有一些外部包可以寫入文件並且它們open()
沒有這些選項,因此這些文件以通用行結束模式打開並最終以 CRLF 寫入。
如何在 Windows 上將默認行為從 CRLF 更改為 LF? 顯然試圖覆蓋os.linesep = '\n'
沒有用。
你可以猴子補丁open
:
import builtins
orig_open = open
def my_open(
file,
mode="r",
buffering=-1,
encoding=None,
errors=None,
newline=None,
closefd=True,
opener=None,
):
return orig_open(
file,
mode,
buffering,
encoding,
errors,
newline="\n",
closefd=closefd,
opener=None,
)
builtins.open = my_open
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.