Python Forum
portable way to get current directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
portable way to get current directory
#1
does Python have a portable way to get the path of the current (working) 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
Yes, I use it all the time
from pathlib import Path
current_directory = Path(__file__).parent
Reply
#3
import os
cwd = os.getcwd()
Recommended Tutorials:
Reply
#4
os stuff even works on Windows?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
From documentation

Output:
os — Miscellaneous operating system interfaces Source code: Lib/os.py This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open(), if you want to manipulate paths, see the os.path module, and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module, and for high-level file and directory handling see the shutil module.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
what if i want to os.dup2() a file descriptor? i didn't know Windows had file descriptors.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get Current Directory From ShortCut. Oshadha 1 923 Jul-30-2022, 04:41 PM
Last Post: snippsat
  Setup Portable Python on Windows for script starts with double clicks? pstein 0 1,778 Feb-18-2022, 01:29 PM
Last Post: pstein
  Portable installation of Python possible? pstein 2 3,004 Nov-15-2020, 12:14 PM
Last Post: snippsat
  where to get portable Python for Linux (Fedora)? python001 5 6,161 Nov-01-2020, 05:23 PM
Last Post: Larz60+
  current directory issue saisankalpj 1 2,388 Nov-12-2018, 12:06 PM
Last Post: buran
  CWD (Current working directory) pcsailor 5 4,601 Nov-11-2018, 10:14 AM
Last Post: Larz60+
  SQlite database portable? PythonWriter 1 2,480 Aug-11-2018, 02:39 PM
Last Post: Larz60+
  Compile python project to single portable .exe file Campbell 5 10,615 Dec-05-2017, 01:20 PM
Last Post: buran
  Importing module from outside of the current directory Annie 4 4,494 Mar-21-2017, 11:13 PM
Last Post: Annie

Forum Jump:

User Panel Messages

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