# Issue with simple code

**URL:** <https://discuss.python.org/t/issue-with-simple-code/104778>\
**Category:** Python Help\
**Tags:** help\
**Created:** [November 8, 2025, 11:51am UTC](https://discuss.python.org/t/issue-with-simple-code/104778 "2025-11-08T11:51:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JP1265](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@JP1265](https://discuss.python.org/u/JP1265)\
**Post date:** [November 8, 2025, 11:51am UTC](https://discuss.python.org/t/issue-with-simple-code/104778/1 "2025-11-08T11:51:16Z")

</div>

Hello, I am pretty unfamiliar with coding in general, but have been coding via spyder for a university unit this past semester. All semester in every class, we have started our scripts with these initial 3 lines:  
import os  
script\_directory = os.path.dirname(os.path.abspath( **file** ))  
os.chdir(script\_directory)

Not sure why, but as of today, every time I go to run these lines I hvae the message:  
Cell In[36], line 1  
----\> 1 script\_directory = os.path.dirname(os.path.abspath( **file** ))

NameError: name ‘ **file** ’ is not defined  
appear in my console.

I am completely confused as to why this is the case considering I have not changed my code whatsoever since yesterday when it was working just fine.

---

<div class="post-metadata">

**Author:** ![pgdr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pgdr/32/31836_2.png) [@pgdr](https://discuss.python.org/u/pgdr)\
**Post date:** [November 8, 2025, 2:40pm UTC](https://discuss.python.org/t/issue-with-simple-code/104778/2 "2025-11-08T14:40:45Z")

</div>

The reason ` __file__ ` doesn’t work, is because Notebooks don’t define that variable.

You _could_ use `os.path.abspath("")` but I don’t know what you want to achieve, so it’s hard to say.

Ps.  
If you use backticks around code, the underscores aren’t eaten up by Markdown.

Also, the double-underscores are called _dunder_.

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [November 8, 2025, 2:59pm UTC](https://discuss.python.org/t/issue-with-simple-code/104778/3 "2025-11-08T14:59:35Z")

</div>

The reason that you’re getting an error is because the argument to `os.path.abspath` is expecting a string argument that represents a path to a file not just the word `file` that has not been assigned a value. If you assign a string value to the word `file` to, say, for example: `file = "/Desktop/myfile.txt`" first, you should be ok (or anything similar).

Passing the argument `file` without it first being assigned a string value will generate an exception (i.e., error) as it shouldt.

> **[Python os.path.abspath() Method](https://www.tutorialspoint.com/python/os_path_abspath.htm)**
>
> The abspath(path) method in the os.path module is used to get the normalized absolutized version of the provided pathname path.

> **[Python | os.path.dirname() method - GeeksforGeeks](https://www.geeksforgeeks.org/python/python-os-path-dirname-method/)**
>
> Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and...

---

<div class="post-metadata">

**Author:** ![c-rob](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/c-rob/32/17261_2.png) [@c-rob](https://discuss.python.org/u/c-rob)\
**Post date:** [November 11, 2025, 10:17am UTC](https://discuss.python.org/t/issue-with-simple-code/104778/4 "2025-11-11T10:17:09Z")

</div>

> [@JP1265](#):
>
> import os  
> script\_directory = os.path.dirname(os.path.abspath( **file** ))  
> os.chdir(script\_directory)

The variable `file` is not defined. You need to define it like this:

```python
import os
file = "c:\myprogram\myfile.txt"
script_directory = os.path.dirname(os.path.abspath(file))
os.chdir(script_directory)

```

1. And why are you changing the directory when you are already in that directory? That seems unnecessary.
2. What are you trying to achieve? This looks like a stub program, or template for other programs that you always start with.

**Help us help you.** Read this link first and learn how to format code so we can help you better. [Formatting code](https://discuss.python.org/t/about-the-python-help-category/42) Do not use a screen shot of your code, instead paste your code in as preformatted code, as some of us will copy and paste the code to get it to work for you, so you can learn from this. You will get more help this way.

---

<div class="post-metadata">

**Author:** ![pgdr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pgdr/32/31836_2.png) [@pgdr](https://discuss.python.org/u/pgdr)\
**Post date:** [November 12, 2025, 10:09pm UTC](https://discuss.python.org/t/issue-with-simple-code/104778/5 "2025-11-12T22:09:26Z")

</div>

I really think op meant ` __file__ ` which, due to lack of backticks, got markdowned to **file**.
