I have 2 files:
First file
test1.py:from dir2.test2 import func2 def func1(): with open("test1.txt", "w") as f: f.write("some text") func1() func2()Second file
test2.py:def func2(): with open("test2.txt", "w") as f: f.write("some text")
And my directory structure looks like this before running test1.py:
dir1
|
- test1.py
|
- dir2
|
- test2.py
After running the test1.py the directory structure look like this:
dir1
|
- test1.txt
|
- test2.txt
|
- test1.py
|
- dir2
|
- test2.py
But after running the script I was expecting the directory structure to look like this:
dir1
|
- test1.txt
|
- test1.py
|
- dir2
|
- test2.txt
|
- test2.py
I have tried searching ways to fix this but haven't found any.
So is there anyway to get something like I was expecting after running test1.py.
./dir2/test2.txtconsidering that you are running the script from withindir1python /home/dev/dir1/test1.pyorC:\Users\You\dev\dir1\test1.py. Python takes in func2 and reads the relative path in the open() call relative to the current working directory (test1.py). This is why it saves the test2.txt file in dir1, not dir2.