do not define an open flag for reading

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: python-best-practices/open-add-flag

Language: Python

Severity: Error

Category: Best Practices

Description

If a flag is opened for read-only, no need to put a flag to open the file.

Learn More

Non-Compliant Code Examples

def print_foo():
  with open("myfile.txt", "r") as myfile:  # no need to specify the "r" flag
    content = myfile.read()

  with open(path, "rb") as f:
    for chunk in iter(lambda: f.read(4096), b""):
        hasher.update(chunk)

Compliant Code Examples

def print_foo():
  with open("myfile.txt") as myfile:
    content = myfile.read()
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis