Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inelegant code!
#2
This is just a fragment of your code; However, I would refactor as follows:
1) use logging instead of printing;
2) Lines No. 8-13: return url? these lines could be pushed to a function, e.g. def get_url(...):
3) requests.get -- may lead to an exception (e.g. if url is bad); you probably need to wrap it with try/except;
4) you don't need to wrap status_code checking with try/except (answer to your question);
5) csv.reader can raise an exception, if loaded content is not csv-compliant; probably, you need to put try/except here. Anyway, I would put these lines into some function.
6) Use with when opening files, e.g. with open(os.path.join('html', AddressFile) as out_file: (in this case you don't need to care about closing the file (out_file.close() isn't needed anymore))
7) Another option is to use pathlib when you need concatenate paths.
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