python3

python3 でファイルに内容を追記する方法

投稿日:2020年11月29日 更新日:

ファイルを開くとき、「a+」を指定することで、data.txt というファイルを書き込みモードで開く。(もしファイルが存在していなければ作成する。)

f = open('data.txt', 'a+')
f.write("追記内容\n")

結果

data.txt
追記内容

-python3
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

python3 でリストを結合する方法

2つのリストを結合して、「リストのリストを作りたい場合」と、「1つの長いリストを作りたい」場合がある。 それぞれ、次のようにする。 例 # リスト2つを用意 l_1 = [1,2,3] l_2 …

no image

matplotlib で折れ線グラフの下部に色をつけて塗りつぶす方法

pyplot の fill_between を使う。 例 from matplotlib import pyplot as plt data_x = [25,26,27] data_y = & …

no image

matplotlib で、グラフの線の幅を変更する方法

lw で、線幅を数字で指定すればよい。 例 from matplotlib import pyplot as plt data_x = [25,26,27] data_y = [39, …

no image

python3 でグローバル変数とローカル変数で同じ名前の変数を使う

グローバル変数とローカル変数で同じ名前の変数を使うことができる。 例 x = 10 def myfunc(): x = 20 print(‘関数内: x = ‘+str(x)) myfunc() pr …

no image

matplotlib で、凡例を表示する方法

データに label を関連付け、plt.legend() で凡例を表示する。 例 from matplotlib import pyplot as plt data_x = [25,26,2 …