Python Forum
CWD (Current working directory)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CWD (Current working directory)
#1
Hi,

At the moment a project I am working on requires me to manually go into the first script and change the CWD to whatever the directory is of the current machine I am running the script on. Fine for me but not so good for other users whom I want to run this script on to help them automate a few processes.

Is it possible to write python code so whichever is the directory the python script is initialized in automatically becomes the current working directory?

Thanks,
phil

SO Link

I found this link but no luck yet.
I've tried this code so far:
os.chdir(os.path.dirname(sys.path[0]))
os.chdir(sys.path[0])
and I'm also trying to use these modules within python itself to change the directory and reviewing some of the docs.
So far no go.

What am I missing?
Thanks,
Phil
Reply
#2
yes, you can use:
import os

os.chdir(os.path.dirname(__file__))
while the program is running, this will be home directory, when you exit the program, path reverts to directory at start

if you are using python 3.6 or better, you can use pathlib to control files in multiple directories, example:

from pathlib import Path
import os


os.chdir(os.path.dirname(__file__))

homepath = Path('.')

rootpath = homepath / '..'

datapath = rootpath / 'data'
datapath.mkdir(exist_ok=True)  # will create directory (only if it doesn't exist, OK otherwise)

tmppath = datapath / 'tmp'
tmppath.mkdir(exist_ok=True)

mytmpfile = tmppath / 'mytmpfile.txt'

with mytmpfile.open('w') ad fp:
    fp.write('Hi, I'm a temp file\n')
Reply
#3
os.chdir(os.path.dirname(__file__))
results in the follinging error:
[WinError 123] The filename, directory name, or volume label syntax is incorrect: ''
the file name is 'page_00_index.py'
The directory is 'D:\software\Python\python_excel\python_openpyxl_dcflog_updated-2018-11-08'

I'm trying to track down why this is now.
Thanks,
Phil
Reply
#4
that code must be run from inside a python script.
and you must import os. Also, you may need to get absoulte path, so I modified as follows.
I also added a print of cwd.

tryme.py:
import os

os.chdir(os.path.abspath(os.path.dirname(__file__)))
print(os.getcwd())
python tryme.py
results:
Output:
(venv) larz60p@Larz60p:/.../Development/development/downloads/w-z/w/wxpython/src>
Reply
#5
Hi Larz,

This did the trick, thank you.

I'm not exactly sure why though. I need to read up on the OS module docs, which I've not gone into in depth.

Thanks again.
Phil
Reply
#6
if you break down:
os.chdir(os.path.abspath(os.path.dirname(__file__)))
os.chdir -- change working directory
os.path.abspath -- return absolute path
os.path.dirname -- return name portion of object.
__file__ -- The path to where the module data is stored (not set for built-in modules).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  working directory if using windows path-variable chitarup 2 779 Nov-28-2023, 11:36 PM
Last Post: chitarup
  Get Current Directory From ShortCut. Oshadha 1 1,004 Jul-30-2022, 04:41 PM
Last Post: snippsat
  Setup working directory in IDLE Pavel_47 3 3,853 Mar-06-2021, 08:59 AM
Last Post: Pavel_47
  Stock Rate of Change (based on open and current price) Not Working firebird 1 2,404 Jul-29-2019, 07:10 AM
Last Post: perfringo
  portable way to get current directory Skaperen 5 3,268 Jun-12-2019, 01:16 PM
Last Post: Skaperen
  setting working directory in pycharm saisankalpj 10 13,292 Dec-06-2018, 07:58 PM
Last Post: snippsat
  current directory issue saisankalpj 1 2,452 Nov-12-2018, 12:06 PM
Last Post: buran
  how to save python file to working directory rpdohm 3 6,072 Sep-14-2017, 06:16 PM
Last Post: rpdohm
  Importing module from outside of the current directory Annie 4 4,643 Mar-21-2017, 11:13 PM
Last Post: Annie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020