0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonのファイルオープンでの"a+"

Posted at

"a+"でファイルの中身を読めなかったけど、やっと使い方を理解したのでメモ

titles = [ "title1", "title2", "title3" ]
with open("file1.txt", "a+") as fd:
    fd.seek(0)
    lines = fd.read().splitlines()
    for title in titles:
        if title in lines:
            print(f"{title} already exist")
        else:
            fd.write(f"{title}\n")
            print(f"{title} added")
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?