Python Forum
simple code: change to home directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple code: change to home directory
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
maybe that works

import os
from os.path import expanduser
myhome = expanduser("~")
### set working dir
os.chdir(myhome)
### get working dir
print(os.getcwd())
Reply
#3
4 lines. i'm comparing to a bash script which just needs 1 line.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
make it two
import os
os.chdir(os.path.expanduser("~"))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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?
Reply
#6
(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 :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [Windows] Change directory date? Winfried 2 1,477 May-11-2025, 04:24 PM
Last Post: Winfried
  Simple code not working properly tmv 2 1,227 Feb-28-2025, 09:27 PM
Last Post: deanhystad
  write code that resides in parent directory franklin97355 3 2,263 Apr-14-2024, 02:03 AM
Last Post: franklin97355
  Help with simple code JacobSkinner 1 1,945 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 1,853 Nov-07-2023, 04:32 PM
Last Post: snippsat
  change directory of save of python files akbarza 3 6,333 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  help me simple code result min and max number abrahimusmaximus 2 2,400 Nov-12-2022, 07:52 AM
Last Post: buran
  home automation using python barryjo 1 2,576 Jul-24-2022, 09:09 PM
Last Post: Larz60+
  Simple encoding code ebolisa 3 3,060 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 3,227 Feb-21-2022, 10:52 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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