Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inelegant code!
#3
One idea which might be worth of considering is to use 'x' (create) mode while opening the file. This will raise FileExistsError if file is already existing.

try:
    with open(filename, 'xt') as f:   # t for text mode, b for binary mode
        # do stuff
except FileExistsError:
    # do other stuff       
Also you may consider pepify/prettify the code using variable naming convetions and f-strings (address_file = f'{date_prefix}-torlist.txt')

Beware of .isnumeric expected behaviour as well:

>>> digits = '123'
>>> numerics = '一二三'   # '123'in Kanji
>>> decimals = '1²'
>>> digits.isnumeric()
True
>>> numerics.isnumeric()
True
>>> decimals.isnumeric()
True
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


Messages In This Thread
Inelegant code! - by howdey57 - Sep-20-2020, 05:31 PM
RE: Inelegant code! - by scidam - Sep-20-2020, 11:35 PM
RE: Inelegant code! - by perfringo - Sep-21-2020, 10:31 AM

Forum Jump:

User Panel Messages

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