Python Forum
How to really change dir in a script ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to really change dir in a script ?
#1
Hi,

I am working with source control software which has a python API.
I want to be able to create a new dir and then populate code from the database to that dir, termed a 'client' by the SC software.
The source control software has an interesting 'feature' that it can create a new 'client' in any location however the command to do that MUST be run from an existing client. So, to try to get around this ridiculous restriction, my idea was run the following python from the <new_dir> location:

import os
import SC # source control API module

os.chdir('<existing_client_dir>')
SC.create_new_client('<new_dir_location>')
os.chdir('<new_dir_location>')
# Mission accomplished !
The problem however is that the call to SC.create_new_client('<new_dir_location>') fails becasue it acts as though it was invoked from <new_dir> despite the preceeding os.chdir('<existing_client_dir>') command. So is there any way to really 'move' the execution of subsequent commands to a new dir ?

If the above example isn't clear I expect what I am asking is equivalent to running the following code from dir1 and ending up in dir2:
dir1> python change_dir.py

where change_dir.py consists of
import os
os.chdir('dir2')
Any ideas ?

Thanks,

Usjes.
Reply
#2
Your program should not change the current working directory.
Use absolute paths and the problem is solved.

If you want to change a location, which should be relative to the old location, you could use pathlib, which is handy for path manipulations. You can do the same with os.path, but it's very low level.

Example with path:

from pathlib import Path



old_ressource_directory = Path('/home/user/pragram_xy/data/dataset_xy')
new_location = old_ressource_directory.parent / 'new_dataset'
The resulting object:
Output:
PosixPath('/home/user/pragram_xy/data/new_dataset')
If you want to access a file in this directory, you can still reuse the object.
resource_file = new_location / 'my_resource.json'
# does the file exsit?
file_exists = resource_file.exist()
# let's read some data
resource_file.read_text()
# same again, but without encoding. Read raw bytes
resource_file.read_bytes()
# or get the stat
resource_file.stat()

# if you work with APIs, which are incopatible with Path objects, then you can convert it to a str.
path_as_str = str(resource_file)
They have better examples: https://realpython.com/python-pathlib/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Change mouse move speed in guibot script rulltartan 1 2,724 Mar-30-2020, 01:51 PM
Last Post: pevogam
  Cannot Change Python script to exe using Pyinstaller omar_mohsen 3 2,385 Dec-19-2019, 01:05 PM
Last Post: buran
  change timing on py script kwfreverie 2 3,115 Dec-16-2017, 07:35 PM
Last Post: kwfreverie

Forum Jump:

User Panel Messages

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