# Get the current working directory in Paraview 5.4.x when running a macro

**URL:** https://discourse.paraview.org/t/get-the-current-working-directory-in-paraview-5-4-x-when-running-a-macro/13348
**Category:** ParaView Support
**Created:** [November 27, 2023, 7:34am UTC](https://discourse.paraview.org/t/get-the-current-working-directory-in-paraview-5-4-x-when-running-a-macro/13348 "2023-11-27T07:34:24Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![madmoonman](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@madmoonman](https://discourse.paraview.org/u/madmoonman)
#### Post date: [November 27, 2023, 7:34am UTC](https://discourse.paraview.org/t/get-the-current-working-directory-in-paraview-5-4-x-when-running-a-macro/13348/1 "2023-11-27T07:34:24Z")

</div>

Hello,  
TL/DR:  
How to get the current working directory in Paraview 5.4.x inside a running macro? os.getcwd() returns the base installation directory of Paraview.

Some background:  
I have been looking around for a while and failed to find a solution to this. We are using py-Macros to automate some stuff. I am currently modernizing a script for a recent Paraview version (5.10.x) but I want to improve some things along the way while keeping it compatible with the version it was originally scripted for: 5.4.1.  
The script involves reading a pvsm state from disc that is positioned in the same folder as the macro. This is an arbitrary folder. Currently, we are required to provide this very directory in the header of the script in order to find required files for the run. The usual way of doing os.getcwd() returns the Paraview base installation path instead of the path the script is located at. Is there another way of accessing the script path while running in the Python shell in Paraview?

Thank you.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [November 27, 2023, 8:28am UTC](https://discourse.paraview.org/t/get-the-current-working-directory-in-paraview-5-4-x-when-running-a-macro/13348/2 "2023-11-27T08:28:12Z")

</div>

> [@madmoonman](#):
>
> the same folder as the macro.

`os.path.dirname(os.path.abspath( __file__ ))`

---

<div class="post-metadata">

### Author: ![madmoonman](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@madmoonman](https://discourse.paraview.org/u/madmoonman)
#### Post date: [November 27, 2023, 8:31am UTC](https://discourse.paraview.org/t/get-the-current-working-directory-in-paraview-5-4-x-when-running-a-macro/13348/3 "2023-11-27T08:31:35Z")

</div>

Hello Mathieu,

this does throw me an error

```auto
NameError: name ' __file__' is not defined

```

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [November 27, 2023, 8:32am UTC](https://discourse.paraview.org/t/get-the-current-working-directory-in-paraview-5-4-x-when-running-a-macro/13348/4 "2023-11-27T08:32:31Z")

</div>

How do you run your macro exactly ?

---

<div class="post-metadata">

### Author: ![madmoonman](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@madmoonman](https://discourse.paraview.org/u/madmoonman)
#### Post date: [November 27, 2023, 8:35am UTC](https://discourse.paraview.org/t/get-the-current-working-directory-in-paraview-5-4-x-when-running-a-macro/13348/5 "2023-11-27T08:35:55Z")

</div>

I open the GUI, Tools → Python Shell (in more recent versions this is under View → …), navigate to the script folder, select the script and hit run. Then it starts running.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [November 27, 2023, 9:20am UTC](https://discourse.paraview.org/t/get-the-current-working-directory-in-paraview-5-4-x-when-running-a-macro/13348/6 "2023-11-27T09:20:51Z")

</div>

Works fine here with ParaView 5.11.1:

```auto
import os
dirname, filename = os.path.split(os.path.abspath( __file__ ))
print(dirname)
print(filename)

```

- run ParaView
- View → Python Shell
- Run Script → file.py

```auto
 /home/glow/data/tmp
a.py

```

---

<div class="post-metadata">

### Author: ![madmoonman](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@madmoonman](https://discourse.paraview.org/u/madmoonman)
#### Post date: [November 27, 2023, 9:31am UTC](https://discourse.paraview.org/t/get-the-current-working-directory-in-paraview-5-4-x-when-running-a-macro/13348/7 "2023-11-27T09:31:04Z")

</div>

I am aware that it works in the recent versions, but I really want to preserve the compatibility of the script for this old version, where it does not work like that. I know its way out of sight, but it would be great to find a solution.
