查看Python中的所有文件处理方法和所有类型的错误。

huangapple go评论111阅读模式
英文:

Viewing all the File Handling Methods and all the types of Errors in Python

问题

如何编写一个代码,我可以使用它来查看Python 3中的所有文件方法,例如close()、detach()、readline()、readlines() ...?我还想编写另一个代码,显示所有可能的错误,例如ArithmeticError、AssertionError、MemoryError ...。我尝试使用dir()来列出预期的输出,但目前还没有成功。我应该如何编写这些代码?

英文:

How would I be able to write a code that I can use to see all of the File Methods like close(), detach(), readline(), readlines() ... in python 3. I also want to have another code that shows all the possible errors like ArithmeticError, AssertionError, MemoryError .... I am trying to use dir() to be able to list the intended outputs but was unsuccessful so far. How would I go about coding or these?

答案1

得分: 1

Open a file and use dir() on the open file object. Remove the entries starting with _ as they are typically implementation details. If you only want callable methods use getattr to look up the attribute in the object and callable to see if it is a method. There will be slightly different lists depending on the mode the file is opened with.

  1. >>> t = open('x.txt', 'w') # text mode
  2. >>> b = open('y.txt', 'wb') # binary mode
  3. >>> print('\n'.join(x for x in dir(t) if callable(getattr(t, x)) and not x.startswith('_')))
  4. close
  5. detach
  6. fileno
  7. flush
  8. isatty
  9. read
  10. readable
  11. readline
  12. readlines
  13. reconfigure
  14. seek
  15. seekable
  16. tell
  17. truncate
  18. writable
  19. write
  20. writelines
  21. >>> print('\n'.join(x for x in dir(b) if callable(getattr(b, x)) and not x.startswith('_')))
  22. close
  23. detach
  24. fileno
  25. flush
  26. isatty
  27. read
  28. read1
  29. readable
  30. readinto
  31. readinto1
  32. readline
  33. readlines
  34. seek
  35. seekable
  36. tell
  37. truncate
  38. writable
  39. write
  40. writelines

For errors, the following works:

  1. >>> print('\n'.join(x for x in dir(__builtins__) if x.endswith('Error')))
  2. ArithmeticError
  3. AssertionError
  4. AttributeError
  5. BlockingIOError
  6. BrokenPipeError
  7. BufferError
  8. ChildProcessError
  9. ConnectionAbortedError
  10. ConnectionError
  11. ConnectionRefusedError
  12. ConnectionResetError
  13. EOFError
  14. EnvironmentError
  15. FileExistsError
  16. FileNotFoundError
  17. FloatingPointError
  18. IOError
  19. ImportError
  20. IndentationError
  21. IndexError
  22. InterruptedError
  23. IsADirectoryError
  24. KeyError
  25. LookupError
  26. MemoryError
  27. ModuleNotFoundError
  28. NameError
  29. NotADirectoryError
  30. NotImplementedError
  31. OSError
  32. OverflowError
  33. PermissionError
  34. ProcessLookupError
  35. RecursionError
  36. ReferenceError
  37. RuntimeError
  38. SyntaxError
  39. SystemError
  40. TabError
  41. TimeoutError
  42. TypeError
  43. UnboundLocalError
  44. UnicodeDecodeError
  45. UnicodeEncodeError
  46. UnicodeError
  47. UnicodeTranslateError
  48. ValueError
  49. WindowsError
  50. ZeroDivisionError
英文:

Open a file and use dir() on the open file object. Remove the entries starting with _ as they are typically implementation details. If you only want callable methods use getattr to look up the attribute in the object and callable to see if it is a method. There will be slightly different lists depending on the mode the file is opened with.

  1. >>> t = open('x.txt', 'w') # text mode
  2. >>> b = open('y.txt', 'wb') # binary mode
  3. >>> print('\n'.join(x for x in dir(t) if callable(getattr(t, x)) and not x.startswith('_')))
  4. close
  5. detach
  6. fileno
  7. flush
  8. isatty
  9. read
  10. readable
  11. readline
  12. readlines
  13. reconfigure
  14. seek
  15. seekable
  16. tell
  17. truncate
  18. writable
  19. write
  20. writelines
  21. >>> print('\n'.join(x for x in dir(b) if callable(getattr(b, x)) and not x.startswith('_')))
  22. close
  23. detach
  24. fileno
  25. flush
  26. isatty
  27. read
  28. read1
  29. readable
  30. readinto
  31. readinto1
  32. readline
  33. readlines
  34. seek
  35. seekable
  36. tell
  37. truncate
  38. writable
  39. write
  40. writelines

For errors, the following works:

  1. >>> print('\n'.join(x for x in dir(__builtins__) if x.endswith('Error')))
  2. ArithmeticError
  3. AssertionError
  4. AttributeError
  5. BlockingIOError
  6. BrokenPipeError
  7. BufferError
  8. ChildProcessError
  9. ConnectionAbortedError
  10. ConnectionError
  11. ConnectionRefusedError
  12. ConnectionResetError
  13. EOFError
  14. EnvironmentError
  15. FileExistsError
  16. FileNotFoundError
  17. FloatingPointError
  18. IOError
  19. ImportError
  20. IndentationError
  21. IndexError
  22. InterruptedError
  23. IsADirectoryError
  24. KeyError
  25. LookupError
  26. MemoryError
  27. ModuleNotFoundError
  28. NameError
  29. NotADirectoryError
  30. NotImplementedError
  31. OSError
  32. OverflowError
  33. PermissionError
  34. ProcessLookupError
  35. RecursionError
  36. ReferenceError
  37. RuntimeError
  38. SyntaxError
  39. SystemError
  40. TabError
  41. TimeoutError
  42. TypeError
  43. UnboundLocalError
  44. UnicodeDecodeError
  45. UnicodeEncodeError
  46. UnicodeError
  47. UnicodeTranslateError
  48. ValueError
  49. WindowsError
  50. ZeroDivisionError

huangapple
  • 本文由 发表于 2023年4月20日 05:39:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定