2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonでのメモ帳で編集したファイルの取り扱い方法

Last updated at Posted at 2019-10-10

すでに作ってある設定ファイルをWindowsのメモ帳で編集したら、その後ファイルが開けなくなりました。破損したかと思ったのですが、何回やっても同じでした...。

理由

utf-8にはBOM(Byte Order Mark)有りと無しの2種類があるようです。

Windows 10 では May2019Update(1903)でメモ帳のデフォルトがUTF-8 BOM無しになりましたが、これ以前のバージョンではBOM有りで保存され変更できません。
(動作確認のためにたまに使う環境だったのでUpdateしていない環境でした)

対応方法

エンコード「utf-8-sig」を使うと良いようです。

ファイルを開く
f = open(path,'r', encoding='utf-8-sig')
ConfigParserを使う
config = configparser.ConfigParser()
config.read(conf,"utf-8-sig")

utf-8-sigはBOM有りも無しも両方きちんとハンドリングできるようですので、Windowsでファイル編集を考慮する必要がある場合は、utf-8-sigを使うと良いでしょう。

参考

むにゃむにゃ

普段Mac使いなのであまり気にしていなかったので、びっくりしました。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?