日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python打印到文件_Python打印到文件

發(fā)布時間:2024/1/1 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python打印到文件_Python打印到文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

python打印到文件

1. Python打印到文件 (1. Python Print to File)

In this lesson, we will see how we can route our python print statements in to a file. This means that whatever we try to print will be saved to a file. This can come handy when:

在本課程中,我們將看到如何將python打印語句路由到文件中。 這意味著我們嘗試打印的任何內(nèi)容都會保存到文件中。 在以下情況下可以派上用場:

  • You don’t want to convert your project to use logger temporarily

    您不想將項目轉(zhuǎn)換為臨時使用記錄器
  • Keep the print statements handy and portable

    保持打印報表方便攜帶

Let’s get started.

讓我們開始吧。

2.打印到文件 (2. Printing to file)

When we need to print our output to files, this can be achieved in two ways which are both simple to use. Let’s look at them one by one.

當我們需要將輸出打印到文件時,可以通過兩種都易于使用的方式來實現(xiàn)。 讓我們一一看一下。

2.1)將路線設(shè)置為全局 (2.1) Setting the route as global)

To start, we can set the output route Python needs to consider as a global variable for our program. This means that you need to make a simple assignment and then call print just like we do any other time.

首先,我們可以設(shè)置Python需要考慮的輸出路徑作為程序的全局變量。 這意味著您需要進行簡單的分配,然后像其他時間一樣調(diào)用print。

Let’s put above logic in a simple code snippet:

讓我們將上面的邏輯放在一個簡單的代碼片段中:

import syssys.stdout = open('output.txt','wt') print("Hello Python!") print("We are printing to file.")

When we run the above program, we see that a new file is created in the same directory with name ‘output.txt’ with following contents:


Let’s try another way of doing this.

當我們運行上述程序時,我們看到在同一個目錄中創(chuàng)建了一個名為'output.txt'的新文件,其內(nèi)容如下:

讓我們嘗試另一種方式。

2.2)確定每個打印呼叫 (2.2) Deciding with each print call)

There can be times when you do not want to set the print to file for the entire program but just for a few calls in the program. This can be achieved as well. Let’s try to achieve this in a simple code snippet:

有時您可能不想為整個程序?qū)⒋蛴≡O(shè)置為文件,而只為程序中的幾次調(diào)用設(shè)置文件。 這也可以實現(xiàn)。 讓我們嘗試通過一個簡單的代碼片段實現(xiàn)這一目標:

print("Hello Python!", file=open('output.txt','a')) print("We are printing to file.", file=open('output.txt','a'))

When we run this program, the same output is achieved. The advantage of this method is that we can decide, with each print call if we want to print the output to a file or not.

當我們運行該程序時,將獲得相同的輸出。 這種方法的優(yōu)點是,我們可以在每次打印調(diào)用時決定是否要將輸出打印到文件中。

3.結(jié)論 (3. Conclusion)

In this quick post, we saw how we can print our statements to a file using Python 3. Although printing to file is cool but we should instead consider logging in Python when we need this kind of behavior with a lot of other customizations.

在這篇快速文章中,我們看到了如何使用Python 3將語句打印到文件中。盡管打印到文件很酷,但是當我們需要這種行為以及許多其他自定義設(shè)置時,我們應(yīng)該考慮使用Python登錄 。

That’s all for routing python print() to file.

這就是將python print()路由到文件的全部內(nèi)容。

翻譯自: https://www.journaldev.com/18424/python-print-to-file

python打印到文件

總結(jié)

以上是生活随笔為你收集整理的python打印到文件_Python打印到文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。