英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论