simple code: change to home directory - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: simple code: change to home directory (/thread-12709.html) |
simple code: change to home directory - Skaperen - Sep-09-2018 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? RE: simple code: change to home directory - Axel_Erfurt - Sep-09-2018 maybe that works import os from os.path import expanduser myhome = expanduser("~") ### set working dir os.chdir(myhome) ### get working dir print(os.getcwd()) RE: simple code: change to home directory - Skaperen - Sep-10-2018 4 lines. i'm comparing to a bash script which just needs 1 line. RE: simple code: change to home directory - buran - Sep-10-2018 make it two import os os.chdir(os.path.expanduser("~")) RE: simple code: change to home directory - Axel_Erfurt - Sep-10-2018 (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? RE: simple code: change to home directory - buran - Sep-10-2018 (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 :-) |