Or like below, although pylint complains about this: "consider using
with". Less indentation this way.
f = None
try:
f = open(FILENAME)
records = f.readlines()
except Exception:
sys.exit(1)
finally:
if f is not None:
f.close()
--
https://mai
On 06/07/2024 12:57, Oscar Benjamin via Python-list wrote:
On Sat, 6 Jul 2024 at 11:55, Rob Cliffe via Python-list
wrote:
Consider this scenario (which I ran into in real life):
I want to open a text file and do a lot of processing on the lines
of that file.
If the file does not e
On 07Jul2024 22:22, Rob Cliffe wrote:
it's legal, but doesn't work (trying to access the file after "with f"
raises the same
ValueError: I/O operation on closed file.
Just to this: of course. The with closes the file. But my version runs
the with after the try/except.
--
https://mail.py
On 07Jul2024 22:22, Rob Cliffe wrote:
Remember, the `open()` call returns a file object _which can be used
as a context manager_. It is separate from the `with` itself.
Did you test this?
f = open(FileName) as f:
is not legal syntax.
No. You're right, remove the "as f:".
it's legal, but
On 07/07/2024 02:08, Cameron Simpson wrote:
On 06Jul2024 11:49, Rob Cliffe wrote:
try:
f = open(FileName) as f:
FileLines = f.readlines()
except FileNotFoundError:
print(f"File {FileName} not found")
sys.exit()
# I forgot to put "f.close()" here -:)
for ln in File Lines:
On 06Jul2024 11:49, Rob Cliffe wrote:
try:
f = open(FileName) as f:
FileLines = f.readlines()
except FileNotFoundError:
print(f"File {FileName} not found")
sys.exit()
# I forgot to put "f.close()" here -:)
for ln in File Lines:
print("I do a lot of processing here")
On 6/07/24 22:49, Rob Cliffe via Python-list wrote:
Consider this scenario (which I ran into in real life):
I want to open a text file and do a lot of processing on the lines
of that file.
If the file does not exist I want to take appropriate action, e.g.
print an error message and ab
My thoughts is that if the "many lines of code" puts the except to far
from the try, then perhaps it would have made sense to factor out some
part there into a function.
Perhaps like:
try:
with open(FileName) as f:
for ln in f{
process(ln)
except FileNotFoundError:
print(f"F
On 7/6/2024 6:49 AM, Rob Cliffe via Python-list wrote:
Consider this scenario (which I ran into in real life):
I want to open a text file and do a lot of processing on the lines
of that file.
If the file does not exist I want to take appropriate action, e.g.
print an error message and
On Sat, 6 Jul 2024 at 11:55, Rob Cliffe via Python-list
wrote:
>
> Consider this scenario (which I ran into in real life):
> I want to open a text file and do a lot of processing on the lines
> of that file.
> If the file does not exist I want to take appropriate action, e.g.
> print an
On 06/07/2024 11:49, Rob Cliffe via Python-list wrote:
> If the file does not exist I want to take appropriate action, e.g.
> print an error message and abort the program.
> I might write it like this:
>
> try:
> with open(FileName) as f:
> for ln in f:
> print("I
On 2024-07-06 at 11:49:06 +0100,
Rob Cliffe via Python-list wrote:
> Is there a better / more Pythonic solution?
https://docs.python.org/3/library/fileinput.html
At least this attempts to abstract the problem of iterating over a file
(or multiple files) into a library routine. I've used it a l
12 matches
Mail list logo