-1
def process_file(self):
        error_flag = 0
        line_count = 0
        log_file = self.file_name
        pure_name = log_file.strip()
        # print('Before opening file ',pure_name)
        logfile_in = open(pure_name, 'r')  # Read file

        lines = logfile_in.readlines()
        # print('After reading file enteries ', pure_name)

Error Message

Traceback (most recent call last):
  File "C:\Users\admin\PycharmProjects\BackupLogCheck\main.py", line 49, in <module>
    backupLogs.process_file()
  File "C:\Users\admin\PycharmProjects\BackupLogCheck\main.py", line 20, in process_file
    lines = logfile_in.readlines()
  File "C:\Users\admin\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 350: character maps to <undefined>

Process finished with exit code 1

Line 49 is where I call above method. But I have traced that it crashes at reading the file. I have checked the file; it has just text in it. I don't know if there are some characters which it doesn't like on reading entries. I am running on Windows 10. I am new to Python, any suggestion how to find/correct the issue?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
user2026794
  • 123
  • 2
  • 13
  • 1
    Maybe try setting the encoding? – dumbPotato21 Mar 03 '22 at 19:10
  • 6
    please provide the error thrown by the program – Vivek Puurkayastha Mar 03 '22 at 19:12
  • 1
    Yes, please provide the _full traceback_ - and put it in your post, not in a comment. – Random Davis Mar 03 '22 at 19:13
  • As above comments, provide detailed error message than just saying it crashes. Have you checked the value of the variable `pure_name` does it point to a valid path to the file that you are trying to read? which OS are you running on, check if the program running has access permission to read that file. there could be many reasons. – Anand Sowmithiran Mar 03 '22 at 19:18
  • For debugging help, you need to make a [mre] including complete code (`pure_name` is not defined), any example input, expected output, and the [full error message with traceback](https://meta.stackoverflow.com/q/359146/4518341). For more tips, see [ask]. – wjandrea Mar 03 '22 at 19:37
  • By the way, how large is your file that you are trying to load, if it is too large to fit in memory, you could see your process crash. see this [question](https://stackoverflow.com/questions/31388839/python-freezes-on-simple-file-read-and-print) – Anand Sowmithiran Mar 03 '22 at 19:56
  • I have provided more code. Also, what is a better way to read large file? This program works file for lot of other files but just doesn't for some files. The file is itself 28kb only. – user2026794 Mar 03 '22 at 22:38
  • 1
    Did you read the error message? It's saying the file contains a `0x90` byte, which is undefined in the CP-1252 codec. So you'll need to specify a different encoding to read the file, or read it as binary if that would work for you. There's still not enough context here for me to say conclusively. Again, you need to make a [mre]. – wjandrea Mar 04 '22 at 22:26
  • https://stackoverflow.com/questions/10487563/unicode-error-handling-with-python-3s-readlines – user2026794 Apr 26 '22 at 18:51

1 Answers1

-2

Try the file name in string format

logfile_in = open('pure_name', 'r')  # Read file
lines = logfile_in.readlines()

print(lines)

output

['test line one\n', 'test line two']

or

logfile_in = open('pure_name', 'r')  # Read file
lines = logfile_in.readlines()

for line in lines:
    print(line)

output

test line one

test line two
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Badrelden Ahmed
  • 106
  • 2
  • 5