do not define an open flag for reading

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

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

PREVIEWING: may/unit-testing