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
  write code that resides in parent directory franklin97355 3 372 Apr-14-2024, 02:03 AM
Last Post: franklin97355
  Help with simple code JacobSkinner 1 314 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 476 Nov-07-2023, 04:32 PM
Last Post: snippsat
  change directory of save of python files akbarza 3 875 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  help me simple code result min and max number abrahimusmaximus 2 902 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,438 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 1,788 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  No Module found in other directory than source code [SOLVED] AlphaInc 1 2,041 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
Photo (Beginners problem) Does file change during this code? fiqu 3 1,887 Nov-03-2021, 10:23 PM
Last Post: bowlofred
  Simple code question about lambda and tuples JasPyt 7 3,299 Oct-04-2021, 05:18 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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