2

I am using VSCode. I have the following setup:

.
├── output
│   
└── src
    ├── foo
    │ 
    └── main.py

I have some code that does a bunch of stuff and then creates an output excel file.

I am storing my results using pandas ExcelWriter:

writer = pd.ExcelWriter('../output/MyOutput.xlsx', engine='xlsxwriter').

Note that the relative path points to the folder output in the root directory.

The file is properly stored in the directory when I run the code using python terminal.

.
├── output
│   └── MyOutput.xlsx
└── src
    ├── foo
    │ 
    └── main.py

However I when running my code using: Run Current File in Python Interactive Window (based on IPython I presume) the file is simply created in the script directory.

.
├── output
│   
└── src
    ├── foo
    │ 
    ├── main.py
    └── MyOutput.xlsx

I believe this has something to do with the way IPython works and is not a bug in my code.

So my question is, how to properly write my code so that both when running the interactive window and the python terminal the output ends up in the desired folder?

Bonus: If my guess is correct and this is the normal IPython behavior, could you point me to some reference or explain how it works?

1
  • This doesn't seem feasible as your example line should have raised an error or created an output/ directory somewhere to put the file in. Having it end up just in src/ should not be possible based on that line, else there's a bug on pandas. Commented Jun 12, 2020 at 17:11

1 Answer 1

1

If you run this code:

import os
print(os.getcwd())

You will find in VSCode and python interactive they have different environments. As os.getcwd() returns the current working directory of a process. and you can refer to this page to know some basic things about python interactive window.

Sign up to request clarification or add additional context in comments.

1 Comment

This explains the root of the problem. What is a good solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.