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

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

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.

>>> t = open('x.txt', 'w')   # text mode
>>> b = open('y.txt', 'wb')  # binary mode
>>> print('\n'.join(x for x in dir(t) if callable(getattr(t, x)) and not x.startswith('_')))
close
detach
fileno
flush
isatty
read
readable
readline
readlines
reconfigure
seek
seekable
tell
truncate
writable
write
writelines
>>> print('\n'.join(x for x in dir(b) if callable(getattr(b, x)) and not x.startswith('_')))
close
detach
fileno
flush
isatty
read
read1
readable
readinto
readinto1
readline
readlines
seek
seekable
tell
truncate
writable
write
writelines

For errors, the following works:

>>> print('\n'.join(x for x in dir(__builtins__) if x.endswith('Error')))
ArithmeticError
AssertionError
AttributeError
BlockingIOError
BrokenPipeError
BufferError
ChildProcessError
ConnectionAbortedError
ConnectionError
ConnectionRefusedError
ConnectionResetError
EOFError
EnvironmentError
FileExistsError
FileNotFoundError
FloatingPointError
IOError
ImportError
IndentationError
IndexError
InterruptedError
IsADirectoryError
KeyError
LookupError
MemoryError
ModuleNotFoundError
NameError
NotADirectoryError
NotImplementedError
OSError
OverflowError
PermissionError
ProcessLookupError
RecursionError
ReferenceError
RuntimeError
SyntaxError
SystemError
TabError
TimeoutError
TypeError
UnboundLocalError
UnicodeDecodeError
UnicodeEncodeError
UnicodeError
UnicodeTranslateError
ValueError
WindowsError
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.

>>> t = open('x.txt', 'w')   # text mode
>>> b = open('y.txt', 'wb')  # binary mode
>>> print('\n'.join(x for x in dir(t) if callable(getattr(t, x)) and not x.startswith('_')))
close
detach
fileno
flush
isatty
read
readable
readline
readlines
reconfigure
seek
seekable
tell
truncate
writable
write
writelines
>>> print('\n'.join(x for x in dir(b) if callable(getattr(b, x)) and not x.startswith('_')))
close
detach
fileno
flush
isatty
read
read1
readable
readinto
readinto1
readline
readlines
seek
seekable
tell
truncate
writable
write
writelines

For errors, the following works:

>>> print('\n'.join(x for x in dir(__builtins__) if x.endswith('Error')))
ArithmeticError
AssertionError
AttributeError
BlockingIOError
BrokenPipeError
BufferError
ChildProcessError
ConnectionAbortedError
ConnectionError
ConnectionRefusedError
ConnectionResetError
EOFError
EnvironmentError
FileExistsError
FileNotFoundError
FloatingPointError
IOError
ImportError
IndentationError
IndexError
InterruptedError
IsADirectoryError
KeyError
LookupError
MemoryError
ModuleNotFoundError
NameError
NotADirectoryError
NotImplementedError
OSError
OverflowError
PermissionError
ProcessLookupError
RecursionError
ReferenceError
RuntimeError
SyntaxError
SystemError
TabError
TimeoutError
TypeError
UnboundLocalError
UnicodeDecodeError
UnicodeEncodeError
UnicodeError
UnicodeTranslateError
ValueError
WindowsError
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:

确定