Python Forum
Reopen file if condition fails
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reopen file if condition fails
#1
Greetings!
I'm trying to re-open a file if "IF" condition fails.
I'm not sure it is the right thing to do (to reopen the file) and also I'm failing to reopen it for some reason.
I do not have any errors.
Here is the code:
from pathlib import Path
import io
import re

for sb in Path('D:\\02\\some_logs\\').iterdir() :
    if sb.is_dir() :
        for esbd in Path(sb).iterdir() :
            if esbd.is_file() :

                with io.open(esbd, encoding='utf-8', errors='ignore') as mfiler: 
                    for el in mfiler :
                        el=el.strip()
                        if re.search("\wLoops\:\s",el)  :                                   ## Loop string
                            *stuff, nbr = el.split(':')  
                            nbr=nbr.strip()
                            if int(nbr) > 1 :
                                pass
                                # Do something here      
                            if int(nbr) <= 1 :
                                with io.open(esbd, encoding='utf-8', errors='ignore') as mfiler:                              
                                    for rel in mfiler :
                                        rel=rel.strip()
                                        print(f"{rel}")
                                        # Do something else 
Thank you!
Reply
#2
tester_V Wrote:I'm not sure it is the right thing to do (to reopen the file)
Why are you reopening the file? I mean what is this code supposed to do? The most obvious thing is that there are too many levels of indentation (11 levels). This is very unusual in Python code. To avoid this, write functions.
Reply
#3
Well,
I'm familiar with functions (know what they do but never used them before) as far as I understand they(functions) would do exactly the same thing as what the indentations do.
Code with indentations is easy to read (for me),
with functions, it becomes complicated, especially for people with minimal knowledge of python (like me) who do not want to complicate things. Not now.

I'm processing very similar files.
They all have a line that tells me which code to use for which file.
The line is "Loops : xx"
where 'xx' is a number of loops. One type of file has a number of loops always more than '1',
and the other is always '1' or less.

I thought I will open each file to check the Loop count, if a file has more than '1' loop I'll process it with one code.
If the number of loops is '1' or '0' I'll process it with a different code.

My thinking is based on what I know about Python, it is very limited...

Thank you.
Reply
#4
You may be the only person who can read code with many levels of indentation and no functions. It is bad programming. Not only is it ugly and difficult to read (Which you could call a personal preference. A preference that nearly all programmers share), but it is difficult to modify.

Eventually you will encounter a problem where you cannot write the code without functions. It will be too complex for you to follow the thread of execution through numerous ifs and else's. It will be too hard to see where variables are modified. The structure of what the program does will be to well hidden. Maybe this is that problem.

And unless you are the only person who will ever use this code it is rude to ignore conventions. I get to work with a lot of really ugly code and I vow to never put anyone else through the same torture. You also risk people unintentionally boycotting your posts on the forum because your code is too hard to work with. Looking at your original post I have no idea what this code is supposed to do, and that makes it difficult to offer any aid. Not even a comment that says what it is meant to do?
ndc85430 and Gribouillis like this post
Reply
#5
Based on your description of the problem, you could structure the code as
def handle_file(path):
    with path.open() as mfiler:
        n = find_loop_count(mfiler)
        mfiler.seek(0) # go back to the beginning of the file
        if n <= 1:
            do_A(mfiler)
        else:
            do_B(mfiler)

def find_loop_count(mfiler):
    ....

def do_A(mfiler):
    ...

def do_B(mfiler):
    ...
tester_V likes this post
Reply
#6
Awesome! Thank you!
Thank you for the code and coaching!
Love you guys!
Reply
#7
Uncle Bob's "step down" rule is a good way to organise functions. You probably want to get your hands on a copy of Clean Code before too long.

The problem of not using functions - or abstractions in general - is that you're exposed to all the details. This makes code harder to read and understand because there's simply too much going on. Abstractions are used well when they hide unimportant details to make the important ones more visible.

It takes practice to do this well and just as it's possible to use too few abstractions, it's also possible to use too many. I've seen this in tests in our codebase, for example - the amount of abstraction makes it really difficult to work out what the tests are actually doing.
Gribouillis likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  else condition not called when if condition is false Sandz1286 10 5,837 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 7,892 Jun-01-2020, 06:00 PM
Last Post: penahuse
  inserting photos in 1 sheet of a 6 sheet excel file fails Pedroski55 0 2,371 Mar-03-2018, 01:53 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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