Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String handling
#21
(Sep-16-2019, 05:11 AM)perfringo Wrote: EDITED: initial code contained error. Blank lines contain newline at end, so line must be stripped otherwise it will be truthy (contains newline). Therefore row.strip() should be used. Below code is fixed.

............

From screenshot I observe that there is blank line at the end of file. Problem can be easily mitigated by little defensive code - 'check whether line is empty'. Also I suggest to open files using 'with', among other goodies there is no need to close file - Python does it for you.

with open('filename.txt', 'r') as f:
    for row in f:
        if row.strip():
            # the rest of code

Oh wow I did not know there is an alternative way to open files, and I was explicitly told to please remember to close them in order to free up the resources they used or something like that, I will look into your method , thanks for the advice and the fix on the code runs like a charm now with no hassle. Much appreciate perfringo

Attached Files

Thumbnail(s)
   
Reply
#22
(Sep-16-2019, 05:23 AM)YoungGrassHopper Wrote: Oh wow I did not know there is an alternative way to open files, and I was explicitly told to please remember to close them in order to free up the resources they used or something like that, I will look into your method , thanks for the advice and the fix on the code runs like a charm now with no hassle. Much appreciate perfringo

From Documentation >>> The Python Tutorial >>> 7. Input and Output >>> 7.2. Reading and Writing Files

Quote:It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point.
/.../
If you’re not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it. If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while. Another risk is that different Python implementations will do this clean-up at different times.

One reason why 'with' is better: while writing the code it can contain errors and blow up. If this happens in code block after open() but before close() this means that file stays open. Some unexpected behaviour can be expected if files are not properly closed (especially if you blow up code again and again). 'with' ensures that file is always closed whatever happens.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#23
(Sep-16-2019, 06:09 AM)perfringo Wrote:
(Sep-16-2019, 05:23 AM)YoungGrassHopper Wrote: Oh wow I did not know there is an alternative way to open files, and I was explicitly told to please remember to close them in order to free up the resources they used or something like that, I will look into your method , thanks for the advice and the fix on the code runs like a charm now with no hassle. Much appreciate perfringo

From Documentation >>> The Python Tutorial >>> 7. Input and Output >>> 7.2. Reading and Writing Files

Quote:It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point.
/.../
If you’re not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it. If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while. Another risk is that different Python implementations will do this clean-up at different times.

One reason why 'with' is better: while writing the code it can contain errors and blow up. If this happens in code block after open() but before close() this means that file stays open. Some unexpected behaviour can be expected if files are not properly closed (especially if you blow up code again and again). 'with' ensures that file is always closed whatever happens.

This is mighty interesting, thanks for the advice perfringo I will try incorporate this "with" method when opening files from now on and refrain from using the other one in the future. sounds more efficient and safe
Reply
#24
Just to expand a bit on with

it's called context manager and is not limited just to opening files.
Many (if not all) database drivers also implement it to work with connection and cursor objects

Also you can easily create one with @contextlib.contextmanager
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,215 Nov-09-2022, 07:29 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020