# Using variable 'file' before assignment

**URL:** <https://forum.codewithmosh.com/t/using-variable-file-before-assignment/20165>\
**Category:** Python\
**Created:** [May 10, 2023, 3:47am UTC](https://forum.codewithmosh.com/t/using-variable-file-before-assignment/20165 "2023-05-10T03:47:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Syavcka](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@Syavcka](https://forum.codewithmosh.com/u/Syavcka)\
**Post date:** [May 10, 2023, 3:47am UTC](https://forum.codewithmosh.com/t/using-variable-file-before-assignment/20165/1 "2023-05-10T03:47:43Z")

</div>

```auto
try:
    file = open("app.py")
    age = int(input("Age: "))
    xfactor = 10 / age
except (ValueError, ZeroDivisionError):
    print("you didn't enter a valid age.")
else:
    print("No exceprions were thrown.")
finally:
    file.close()

```

PyLint highlights with red “file.close()” in finally block and issues a warning “Using variable ‘file’ before assignmentPylint(E0601:used-before-assignment)”. I need help to understand what’s wrong) I tried to execute this code a few times, and everything worked great.

---

<div class="post-metadata">

**Author:** ![Juvelix](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/juvelix/32/6169_2.png) [@Juvelix](https://forum.codewithmosh.com/u/Juvelix)\
**Post date:** [May 18, 2023, 4:46pm UTC](https://forum.codewithmosh.com/t/using-variable-file-before-assignment/20165/2 "2023-05-18T16:46:35Z")

</div>

To address this issue, you can declare the `file` variable  
Example at the beginning:

#Initialize the variable with a default value  
file = None or File = “”

Info  
E0601 used before assignment:  
Description: **Emitted when a local variable is accessed before its assignment took place**. Assignments in try blocks are assumed not to have occurred when evaluating associated except/finally blocks.

Details:  
[https://pylint.readthedocs.io/en/latest/user\_guide/messages/error/used-before-assignment.html](https://pylint.readthedocs.io/en/latest/user_guide/messages/error/used-before-assignment.html)

---

<div class="post-metadata">

**Author:** ![alyssamorgan](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@alyssamorgan](https://forum.codewithmosh.com/u/alyssamorgan)\
**Post date:** [November 24, 2023, 9:58am UTC](https://forum.codewithmosh.com/t/using-variable-file-before-assignment/20165/3 "2023-11-24T09:58:38Z")

</div>

Seems like the the warning you’re getting, Using variable ‘file’ before assignment (E0601:used-before-assignment), you mistakenly edited your code as it worked great. You may be using the wrong variable. Don’t worry, if you are still not getting it. You can prefer [CIPD level 3 assignment help UK](https://cipdassignmentwriting.co.uk/level-3) to write your assignment for you.
