Reputation: 111
Hello I'm writing a small script which will make a webbsite template for me. I'm going to make an alias which would run the script and thats why I have a problem. When running the script, I don't want to write a directory to where I want to create my folder. I want it to create in my terminals working directory. How do I "os.chdir()" to my terminals working directory. I have tried:
import os
dir_path = os.system("pwd | clip")
os.chdir(dir_path)
Output:
TypeError: chdir: path should be string, bytes or os.PathLike, not int
TL,DR I want to get the path from where in terminal the script executes and then os.chdir() to it
Upvotes: 4
Views: 8346
Reputation: 5330
Get current working dir:
import os
cwd = os.getcwd()
Get the dir of the file you are executing from:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
You can manipulate the string with e.g. dir_path = os.path.join(dir_path, '..', 'somefolder')
.
With the resulting string you can do os.chdir(dir_path)
Upvotes: 8