File Name issue
Mladen Gogala
mgogala at yahoo.com
Sun Oct 18 12:58:32 EDT 2020
On Sun, 18 Oct 2020 21:00:18 +1300, dn wrote:
> On 18/10/2020 12:58, Mladen Gogala via Python-list wrote:
>> On Sat, 17 Oct 2020 22:51:11 +0000, Mladen Gogala wrote:
>>> On Sat, 17 Oct 2020 18:12:16 -0400, Steve wrote:
>>>
>>>> with open("HOURLYLOG.txt", 'r') as infile:
>>>> works but, when I rename the file, the line:
>>>> with open("HOURLY-LOG.txt", 'r') as infile:
>>>> does not. The complaint is: Cannot Assign to operator
>
> >>> with open( "HOURLY-LOG.txt", "r" ) as f:
> ... print( f.read() )
> ...
> yes
> ^^^ the entire contents of a magnificent test-file
>
> NB:
> Python 3.8.5 (default, Aug 12 2020, 00:00:00)
> [GCC 10.2.1 20200723 (Red Hat 10.2.1-1)] on linux
>
>
>> BTW, I used this
>> cp /var/log/syslog ./in-file.log
>> #!/usr/bin/env python3
>> import io
>> with open("in-file.log","r") as infile:
>> for line in infile:
>> print(line)
>> I got a different error:
>> Traceback (most recent call last):
>> File "./test.py", line 4, in <module>
>> for line in infile:
>> File "/usr/lib/python3.8/codecs.py", line 322, in decode
>> (result, consumed) = self._buffer_decode(data, self.errors, final)
>> UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd8 in position
897:
>> invalid continuation byte
>
>
> @Mladen: is syslog a text file or binary format?
Hi!
Syslog is the system log. It's a text file. This only happens if I use
infile as iterable. If I use readline, all is well:
#!/usr/bin/env python3
import io
with open("in-file.log","r") as infile:
while True:
line=infile.readline()
if not line:
break
print(line)
I don't particularly like this idiom, but it works. That is probably a bug
in the utf-8 decoder on Ubuntu. It doesn't happen on my Fedora 32 VM. I
haven't tried with infile.reconfigure(encoding=None)
--
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com
More information about the Python-list
mailing list