I have encountered something, I am building a command line tool in python.
Now I use the os.chdir
to change the current working directory (with command cd
).
I noticed that when I put ..
in the input, it brings me back in the directory tree.
For example, I am in directory D:\pythonProject
I input cd ..
So the chdir parameter is D:\pythonProject\..
(I am using os.path.join). And then, the current working directory becomes D:
. Obviously I can't go back more than that. But why does this happen? I didn't include handling for ..
in my code. Is it something that is built-in?
This is my function:
def change_working_dir(path):
path_dest = os.path.join(os.getcwd(), path)
if os.path.exists(path_dest):
if os.path.isdir(path_dest):
os.chdir(path_dest)
elif os.path.isfile(path_dest):
os.startfile(path_dest)
else:
print("No such path.")
Thanks!