Python Forum

Full Version: simple code: change to home directory
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
in a script/app that expects to access many (read or write) files in the home directory, what is the simplest code to switch the current directory to be the user's (who runs it) home directory. "simple" does not necessarily mean making it be a one liner. if the user is trying to debug the code and thinks maybe the change to the home directory is not working right, "simpler" makes easier for someone to debug it. and how well does it handle error cases, such as wrong permissions that do not allow switch to the home directory?
maybe that works

import os
from os.path import expanduser
myhome = expanduser("~")
### set working dir
os.chdir(myhome)
### get working dir
print(os.getcwd())
4 lines. i'm comparing to a bash script which just needs 1 line.
make it two
import os
os.chdir(os.path.expanduser("~"))
(Sep-10-2018, 07:13 AM)Skaperen Wrote: [ -> ]4 lines. i'm comparing to a bash script which just needs 1 line.

And what are you using?
(Sep-09-2018, 03:37 AM)Skaperen Wrote: [ -> ]"simple" does not necessarily mean making it be a one liner.
Given the above Skaperen is as usual just chatty :-)