Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confused with 'flags'
#10
The term "flags" is used to mean a lot of different things, but I think you are talking about using a variable to signal that something happened. In this context flags are just variables. You can use a flag in an if-else statement, but a flag is not a substitute for else.
found = False  # I have not found a line containing 123
with open('C:/02/somefile.txt','r') as file:
    for line in file:
        if '123' in line: 
            found = True  # I found a line
            break

if found:  # Did I find a line?
    print(line)
    # Do lots of additional processing
A flag is used when there is space between finding and acting on the condition you are interested in. In the example below verbose is a flag used to signal that '--verbose' was contained in the command line arguments.
import sys

verbose = '--verbose' in sys.argv:

with open(sys.args[1], 'r') as file:
    for line in file:
        if verbose:
            print(line)
        if '123' in line: 
            break
tester_V likes this post
Reply


Messages In This Thread
Confused with 'flags' - by tester_V - Apr-10-2021, 08:31 PM
RE: Confused with 'flags' - by jefsummers - Apr-10-2021, 09:00 PM
RE: Confused with 'flags' - by tester_V - Apr-10-2021, 09:27 PM
RE: Confused with 'flags' - by menator01 - Apr-10-2021, 11:18 PM
RE: Confused with 'flags' - by tester_V - Apr-10-2021, 11:56 PM
RE: Confused with 'flags' - by menator01 - Apr-11-2021, 03:09 AM
RE: Confused with 'flags' - by tester_V - Apr-11-2021, 03:47 AM
RE: Confused with 'flags' - by ndc85430 - Apr-11-2021, 04:56 AM
RE: Confused with 'flags' - by tester_V - Apr-11-2021, 06:00 AM
RE: Confused with 'flags' - by deanhystad - Apr-11-2021, 01:15 PM
RE: Confused with 'flags' - by tester_V - Apr-12-2021, 03:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String int confused janeik 7 1,087 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 977 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Pandas confused DPaul 6 2,581 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,731 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Regex - Pass Flags as a function argument? muzikman 6 3,618 Sep-06-2021, 03:43 PM
Last Post: muzikman
  Passing flags to python script, through a function xbit 4 3,995 Apr-20-2021, 06:32 AM
Last Post: ndc85430
  Simple Tic Tac Toe but I'm confused Izith 1 2,205 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 3,039 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused on how to go about writing this or doing this... pythonforumuser 3 2,503 Feb-10-2020, 09:15 AM
Last Post: snippsat
  Dazed and confused... RodNintendeaux 10 7,533 May-28-2017, 01:32 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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