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()