简体   繁体   中英

Open a .dir file using any python

I have a file with .dir extension. I tried opening it but getting unknown/unreadable characters. I also tried opening it using python and java but not able to get the correct encoding/decoding for the characters in file. Can someone help me in this or provide some other application in which I can open this file? I have tried the below code in python but getting unreadable characters:

with open(file_name, "rb") as binary_file:
    data = binary_file.read()
    dec_str = data.decode('utf-8', errors='ignore')
    print(dec_str)

You can use the lovely chardet package to infer file encoding.

import chardet

with open(file_name, "rb") as binary_file:
    data = binary_file.read()
    enc = chardet.detect(data)
    dec_str = data.decode(enc['encoding'], errors='ignore')
    print(dec_str)

See official docs for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM