共計 241 個字符,預計需要花費 1 分鐘才能閱讀完成。
要取消 Python 寫入文件時的自動換行,可以通過在寫入操作時指定 end=""
來實現(xiàn)。下面是一個示例:
# 打開文件,以寫入模式打開,如果文件不存在則創(chuàng)建新文件
file = open("example.txt", "w")
# 寫入內(nèi)容到文件中
file.write("Hello")
file.write("World")
# 關閉文件
file.close()
運行以上代碼后,example.txt
文件中的內(nèi)容將會是HelloWorld
,而不是Hello\nWorld
。
丸趣 TV 網(wǎng) – 提供最優(yōu)質的資源集合!
正文完