How to create a file in python in one directory up without using the full path?
I need a way that worked for both windows and linux.
Thank.
Use os.pardir (which is probably always ".." )
os.pardir
".."
import os fobj = open(os.path.join(os.pardir, "filename"), "w")
People don't seem to understand this, but Python is happy to accept the slash even on Windows. This works great on all platforms:
fobj = open("../filename", "w")
Depends on whether you work in a unix or windows environment.
In the windows:
..\foo.txt
On unix as OS:
../foo.txt
you need to make sure that os sets the current path correctly when starting the application. Take the appropriate path and just create a file there.