---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/remove_newline_after_code_block_open.py
---
## Input

```python
import random


def foo1():

    print("The newline above me should be kept!")


def foo2():



    print("All the newlines above me should be kept!")


def foo3():

    print("No newline above me!")

    print("There is a newline above me, and that's OK!")


def foo4():

    # There is a comment here

    print("The newline above me should not be deleted!")


class Foo:
    def bar(self):

        print("The newline above me should be kept!")


for i in range(5):

    print(f"{i}) The line above me should be kept!")


for i in range(5):



    print(f"{i}) The lines above me should be kept!")


for i in range(5):

    for j in range(7):

        print(f"{i}) The lines above me should be kept!")


if random.randint(0, 3) == 0:

    print("The new line above me will be kept!")


if random.randint(0, 3) == 0:




    print("The new lines above me will be kept!")


if random.randint(0, 3) == 0:

    if random.uniform(0, 1) > 0.5:

        print("Two lines above me will be kept!")


while True:

    print("The newline above me should be kept!")


while True:



    print("The newlines above me should be kept!")


while True:

    while False:

        print("The newlines above me should be kept!")


with open("/path/to/file.txt", mode="w") as file:

    file.write("The new line above me will be kept!")


with open("/path/to/file.txt", mode="w") as file:



    file.write("The new lines above me will be kept!")


with open("/path/to/file.txt", mode="r") as read_file:

    with open("/path/to/output_file.txt", mode="w") as write_file:

        write_file.writelines(read_file.readlines())
```

## Black Differences

```diff
--- Black
+++ Ruff
@@ -2,24 +2,20 @@
 
 
 def foo1():
-
     print("The newline above me should be kept!")
 
 
 def foo2():
-
     print("All the newlines above me should be kept!")
 
 
 def foo3():
-
     print("No newline above me!")
 
     print("There is a newline above me, and that's OK!")
 
 
 def foo4():
-
     # There is a comment here
 
     print("The newline above me should not be deleted!")
@@ -27,73 +23,56 @@
 
 class Foo:
     def bar(self):
-
         print("The newline above me should be kept!")
 
 
 for i in range(5):
-
     print(f"{i}) The line above me should be kept!")
 
 
 for i in range(5):
-
     print(f"{i}) The lines above me should be kept!")
 
 
 for i in range(5):
-
     for j in range(7):
-
         print(f"{i}) The lines above me should be kept!")
 
 
 if random.randint(0, 3) == 0:
-
     print("The new line above me will be kept!")
 
 
 if random.randint(0, 3) == 0:
-
     print("The new lines above me will be kept!")
 
 
 if random.randint(0, 3) == 0:
-
     if random.uniform(0, 1) > 0.5:
-
         print("Two lines above me will be kept!")
 
 
 while True:
-
     print("The newline above me should be kept!")
 
 
 while True:
-
     print("The newlines above me should be kept!")
 
 
 while True:
-
     while False:
-
         print("The newlines above me should be kept!")
 
 
 with open("/path/to/file.txt", mode="w") as file:
-
     file.write("The new line above me will be kept!")
 
 
 with open("/path/to/file.txt", mode="w") as file:
-
     file.write("The new lines above me will be kept!")
 
 
 with open("/path/to/file.txt", mode="r") as read_file:
-
     with open("/path/to/output_file.txt", mode="w") as write_file:
-
         write_file.writelines(read_file.readlines())
```

## Ruff Output

```python
import random


def foo1():
    print("The newline above me should be kept!")


def foo2():
    print("All the newlines above me should be kept!")


def foo3():
    print("No newline above me!")

    print("There is a newline above me, and that's OK!")


def foo4():
    # There is a comment here

    print("The newline above me should not be deleted!")


class Foo:
    def bar(self):
        print("The newline above me should be kept!")


for i in range(5):
    print(f"{i}) The line above me should be kept!")


for i in range(5):
    print(f"{i}) The lines above me should be kept!")


for i in range(5):
    for j in range(7):
        print(f"{i}) The lines above me should be kept!")


if random.randint(0, 3) == 0:
    print("The new line above me will be kept!")


if random.randint(0, 3) == 0:
    print("The new lines above me will be kept!")


if random.randint(0, 3) == 0:
    if random.uniform(0, 1) > 0.5:
        print("Two lines above me will be kept!")


while True:
    print("The newline above me should be kept!")


while True:
    print("The newlines above me should be kept!")


while True:
    while False:
        print("The newlines above me should be kept!")


with open("/path/to/file.txt", mode="w") as file:
    file.write("The new line above me will be kept!")


with open("/path/to/file.txt", mode="w") as file:
    file.write("The new lines above me will be kept!")


with open("/path/to/file.txt", mode="r") as read_file:
    with open("/path/to/output_file.txt", mode="w") as write_file:
        write_file.writelines(read_file.readlines())
```

## Black Output

```python
import random


def foo1():

    print("The newline above me should be kept!")


def foo2():

    print("All the newlines above me should be kept!")


def foo3():

    print("No newline above me!")

    print("There is a newline above me, and that's OK!")


def foo4():

    # There is a comment here

    print("The newline above me should not be deleted!")


class Foo:
    def bar(self):

        print("The newline above me should be kept!")


for i in range(5):

    print(f"{i}) The line above me should be kept!")


for i in range(5):

    print(f"{i}) The lines above me should be kept!")


for i in range(5):

    for j in range(7):

        print(f"{i}) The lines above me should be kept!")


if random.randint(0, 3) == 0:

    print("The new line above me will be kept!")


if random.randint(0, 3) == 0:

    print("The new lines above me will be kept!")


if random.randint(0, 3) == 0:

    if random.uniform(0, 1) > 0.5:

        print("Two lines above me will be kept!")


while True:

    print("The newline above me should be kept!")


while True:

    print("The newlines above me should be kept!")


while True:

    while False:

        print("The newlines above me should be kept!")


with open("/path/to/file.txt", mode="w") as file:

    file.write("The new line above me will be kept!")


with open("/path/to/file.txt", mode="w") as file:

    file.write("The new lines above me will be kept!")


with open("/path/to/file.txt", mode="r") as read_file:

    with open("/path/to/output_file.txt", mode="w") as write_file:

        write_file.writelines(read_file.readlines())
```
