Opening a CSV file in Python, with error handling











up vote
1
down vote

favorite
1












Trying to improve my function, as will be used by most of my code. I'm handling most common exception (IOError) and handling when data has no values.



READ_MODE = 'r'


def _ReadCsv(filename):
"""Read CSV file from remote path.

Args:
filename(str): filename to read.
Returns:
The contents of CSV file.
Raises:
ValueError: Unable to read file
"""
try:
with open(filename, READ_MODE) as input_file:
data = input_file.read()
if not data:
raise ValueError('No data available')
except IOError as e:
logging.exception(e)
return data









share|improve this question




















  • 4




    It's incorrect to to say you were "Unable to read file" when you successfully read an empty file.
    – Peilonrayz
    Aug 14 '17 at 8:07






  • 1




    Why not use Python's own csv module: devdocs.io/python~2.7/library/csv
    – hjpotter92
    Aug 14 '17 at 8:10






  • 1




    Corrected message.
    – spicyramen
    Aug 14 '17 at 16:21















up vote
1
down vote

favorite
1












Trying to improve my function, as will be used by most of my code. I'm handling most common exception (IOError) and handling when data has no values.



READ_MODE = 'r'


def _ReadCsv(filename):
"""Read CSV file from remote path.

Args:
filename(str): filename to read.
Returns:
The contents of CSV file.
Raises:
ValueError: Unable to read file
"""
try:
with open(filename, READ_MODE) as input_file:
data = input_file.read()
if not data:
raise ValueError('No data available')
except IOError as e:
logging.exception(e)
return data









share|improve this question




















  • 4




    It's incorrect to to say you were "Unable to read file" when you successfully read an empty file.
    – Peilonrayz
    Aug 14 '17 at 8:07






  • 1




    Why not use Python's own csv module: devdocs.io/python~2.7/library/csv
    – hjpotter92
    Aug 14 '17 at 8:10






  • 1




    Corrected message.
    – spicyramen
    Aug 14 '17 at 16:21













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





Trying to improve my function, as will be used by most of my code. I'm handling most common exception (IOError) and handling when data has no values.



READ_MODE = 'r'


def _ReadCsv(filename):
"""Read CSV file from remote path.

Args:
filename(str): filename to read.
Returns:
The contents of CSV file.
Raises:
ValueError: Unable to read file
"""
try:
with open(filename, READ_MODE) as input_file:
data = input_file.read()
if not data:
raise ValueError('No data available')
except IOError as e:
logging.exception(e)
return data









share|improve this question















Trying to improve my function, as will be used by most of my code. I'm handling most common exception (IOError) and handling when data has no values.



READ_MODE = 'r'


def _ReadCsv(filename):
"""Read CSV file from remote path.

Args:
filename(str): filename to read.
Returns:
The contents of CSV file.
Raises:
ValueError: Unable to read file
"""
try:
with open(filename, READ_MODE) as input_file:
data = input_file.read()
if not data:
raise ValueError('No data available')
except IOError as e:
logging.exception(e)
return data






python error-handling csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 14 '17 at 22:26









200_success

127k15148411




127k15148411










asked Aug 14 '17 at 7:07









spicyramen

265311




265311








  • 4




    It's incorrect to to say you were "Unable to read file" when you successfully read an empty file.
    – Peilonrayz
    Aug 14 '17 at 8:07






  • 1




    Why not use Python's own csv module: devdocs.io/python~2.7/library/csv
    – hjpotter92
    Aug 14 '17 at 8:10






  • 1




    Corrected message.
    – spicyramen
    Aug 14 '17 at 16:21














  • 4




    It's incorrect to to say you were "Unable to read file" when you successfully read an empty file.
    – Peilonrayz
    Aug 14 '17 at 8:07






  • 1




    Why not use Python's own csv module: devdocs.io/python~2.7/library/csv
    – hjpotter92
    Aug 14 '17 at 8:10






  • 1




    Corrected message.
    – spicyramen
    Aug 14 '17 at 16:21








4




4




It's incorrect to to say you were "Unable to read file" when you successfully read an empty file.
– Peilonrayz
Aug 14 '17 at 8:07




It's incorrect to to say you were "Unable to read file" when you successfully read an empty file.
– Peilonrayz
Aug 14 '17 at 8:07




1




1




Why not use Python's own csv module: devdocs.io/python~2.7/library/csv
– hjpotter92
Aug 14 '17 at 8:10




Why not use Python's own csv module: devdocs.io/python~2.7/library/csv
– hjpotter92
Aug 14 '17 at 8:10




1




1




Corrected message.
– spicyramen
Aug 14 '17 at 16:21




Corrected message.
– spicyramen
Aug 14 '17 at 16:21










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










The only purpose of using a try/except that does not re-raise would be if it doesn't matter if there's an IOError. However, in this case, if an IOError occurs, it will be logged, then ignored and then return is going to raise a NameError - that 'data' is not defined. Since you are raising an error on empty results, I assume you handle that in the caller or want it to stop the process. In either case, I'd do something like



import logging
READ_MODE = 'r'


def _ReadCsv(filename):
"""Read CSV file from remote path.

Args:
filename(str): filename to read.
Returns:
The contents of CSV file.
Raises:
ValueError: Unable to read file
"""
data = None
try:
with open(filename) as fobj:
data = fobj.read()
except IOError:
logging.exception('')
if not data:
raise ValueError('No data available')
return data


Also, there's no need for the with open construct if you are just reading it. With open is preferred if you do more than one thing with the file object, so it gets closed properly. By not assigning the file open to a variable, it will get closed properly and garbage collected. Keep the with open, since garbage collection is an implementation detail you should not rely on any particular behavior.



With logging.exception, all the neat stuff that you see people doing manually is already taken care of. By simply calling it with an empty string, you get the full traceback, exception type and text without doing anything else.



logging.exception('')


Is equivalent to



logging.error(''.join(traceback.format_exception(*sys.exc_info)))


or



logging.error('', exc_info=True)





share|improve this answer






























    up vote
    3
    down vote













    Don't handle the exception in this part of the code, since it will hide errors. Just let it bubble up.






    share|improve this answer





















      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "196"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f172936%2fopening-a-csv-file-in-python-with-error-handling%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      The only purpose of using a try/except that does not re-raise would be if it doesn't matter if there's an IOError. However, in this case, if an IOError occurs, it will be logged, then ignored and then return is going to raise a NameError - that 'data' is not defined. Since you are raising an error on empty results, I assume you handle that in the caller or want it to stop the process. In either case, I'd do something like



      import logging
      READ_MODE = 'r'


      def _ReadCsv(filename):
      """Read CSV file from remote path.

      Args:
      filename(str): filename to read.
      Returns:
      The contents of CSV file.
      Raises:
      ValueError: Unable to read file
      """
      data = None
      try:
      with open(filename) as fobj:
      data = fobj.read()
      except IOError:
      logging.exception('')
      if not data:
      raise ValueError('No data available')
      return data


      Also, there's no need for the with open construct if you are just reading it. With open is preferred if you do more than one thing with the file object, so it gets closed properly. By not assigning the file open to a variable, it will get closed properly and garbage collected. Keep the with open, since garbage collection is an implementation detail you should not rely on any particular behavior.



      With logging.exception, all the neat stuff that you see people doing manually is already taken care of. By simply calling it with an empty string, you get the full traceback, exception type and text without doing anything else.



      logging.exception('')


      Is equivalent to



      logging.error(''.join(traceback.format_exception(*sys.exc_info)))


      or



      logging.error('', exc_info=True)





      share|improve this answer



























        up vote
        2
        down vote



        accepted










        The only purpose of using a try/except that does not re-raise would be if it doesn't matter if there's an IOError. However, in this case, if an IOError occurs, it will be logged, then ignored and then return is going to raise a NameError - that 'data' is not defined. Since you are raising an error on empty results, I assume you handle that in the caller or want it to stop the process. In either case, I'd do something like



        import logging
        READ_MODE = 'r'


        def _ReadCsv(filename):
        """Read CSV file from remote path.

        Args:
        filename(str): filename to read.
        Returns:
        The contents of CSV file.
        Raises:
        ValueError: Unable to read file
        """
        data = None
        try:
        with open(filename) as fobj:
        data = fobj.read()
        except IOError:
        logging.exception('')
        if not data:
        raise ValueError('No data available')
        return data


        Also, there's no need for the with open construct if you are just reading it. With open is preferred if you do more than one thing with the file object, so it gets closed properly. By not assigning the file open to a variable, it will get closed properly and garbage collected. Keep the with open, since garbage collection is an implementation detail you should not rely on any particular behavior.



        With logging.exception, all the neat stuff that you see people doing manually is already taken care of. By simply calling it with an empty string, you get the full traceback, exception type and text without doing anything else.



        logging.exception('')


        Is equivalent to



        logging.error(''.join(traceback.format_exception(*sys.exc_info)))


        or



        logging.error('', exc_info=True)





        share|improve this answer

























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          The only purpose of using a try/except that does not re-raise would be if it doesn't matter if there's an IOError. However, in this case, if an IOError occurs, it will be logged, then ignored and then return is going to raise a NameError - that 'data' is not defined. Since you are raising an error on empty results, I assume you handle that in the caller or want it to stop the process. In either case, I'd do something like



          import logging
          READ_MODE = 'r'


          def _ReadCsv(filename):
          """Read CSV file from remote path.

          Args:
          filename(str): filename to read.
          Returns:
          The contents of CSV file.
          Raises:
          ValueError: Unable to read file
          """
          data = None
          try:
          with open(filename) as fobj:
          data = fobj.read()
          except IOError:
          logging.exception('')
          if not data:
          raise ValueError('No data available')
          return data


          Also, there's no need for the with open construct if you are just reading it. With open is preferred if you do more than one thing with the file object, so it gets closed properly. By not assigning the file open to a variable, it will get closed properly and garbage collected. Keep the with open, since garbage collection is an implementation detail you should not rely on any particular behavior.



          With logging.exception, all the neat stuff that you see people doing manually is already taken care of. By simply calling it with an empty string, you get the full traceback, exception type and text without doing anything else.



          logging.exception('')


          Is equivalent to



          logging.error(''.join(traceback.format_exception(*sys.exc_info)))


          or



          logging.error('', exc_info=True)





          share|improve this answer














          The only purpose of using a try/except that does not re-raise would be if it doesn't matter if there's an IOError. However, in this case, if an IOError occurs, it will be logged, then ignored and then return is going to raise a NameError - that 'data' is not defined. Since you are raising an error on empty results, I assume you handle that in the caller or want it to stop the process. In either case, I'd do something like



          import logging
          READ_MODE = 'r'


          def _ReadCsv(filename):
          """Read CSV file from remote path.

          Args:
          filename(str): filename to read.
          Returns:
          The contents of CSV file.
          Raises:
          ValueError: Unable to read file
          """
          data = None
          try:
          with open(filename) as fobj:
          data = fobj.read()
          except IOError:
          logging.exception('')
          if not data:
          raise ValueError('No data available')
          return data


          Also, there's no need for the with open construct if you are just reading it. With open is preferred if you do more than one thing with the file object, so it gets closed properly. By not assigning the file open to a variable, it will get closed properly and garbage collected. Keep the with open, since garbage collection is an implementation detail you should not rely on any particular behavior.



          With logging.exception, all the neat stuff that you see people doing manually is already taken care of. By simply calling it with an empty string, you get the full traceback, exception type and text without doing anything else.



          logging.exception('')


          Is equivalent to



          logging.error(''.join(traceback.format_exception(*sys.exc_info)))


          or



          logging.error('', exc_info=True)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 11 mins ago

























          answered Aug 14 '17 at 17:35









          Wyrmwood

          1363




          1363
























              up vote
              3
              down vote













              Don't handle the exception in this part of the code, since it will hide errors. Just let it bubble up.






              share|improve this answer

























                up vote
                3
                down vote













                Don't handle the exception in this part of the code, since it will hide errors. Just let it bubble up.






                share|improve this answer























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  Don't handle the exception in this part of the code, since it will hide errors. Just let it bubble up.






                  share|improve this answer












                  Don't handle the exception in this part of the code, since it will hide errors. Just let it bubble up.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 14 '17 at 7:12









                  Roland Illig

                  10.6k11844




                  10.6k11844






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f172936%2fopening-a-csv-file-in-python-with-error-handling%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Feedback on college project

                      Futebolista

                      Albești (Vaslui)