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