Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6209723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:02:11+00:00 2026-05-24T06:02:11+00:00

import os print __file__ print os.path.dirname(__file__) os.chdir(‘/tmp’) print __file__ # unchanged, of course print

  • 0
import os
print __file__
print os.path.dirname(__file__)
os.chdir('/tmp')
print __file__  # unchanged, of course
print os.path.dirname(__file__)  # now broken

I have this issue above where dirname(__file__) can no longer be relied upon after os.chdir has been used in the script, after module loader has set __file__.

What is the usual mechanism for working around this, assuming you may not know where/when/how os.chdir may have been called previously?

edit: i hope this second example can better clarify my issue

import os
old_dir = os.getcwd()
print os.path.abspath(__file__)
os.chdir('/tmp')
print os.path.abspath(__file__)
os.chdir(old_dir)

the output is like this :

wim@wim-acer:~$ python --version
Python 2.7.1+
wim@wim-acer:~$ pwd
/home/wim
wim@wim-acer:~$ python /home/wim/spam.py
/home/wim/spam.py
/home/wim/spam.py
wim@wim-acer:~$ python ./spam.py
/home/wim/spam.py
/tmp/spam.py
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:02:12+00:00Added an answer on May 24, 2026 at 6:02 am

    The __file__ must exist in sys.path somewhere.

    for dirname in sys.path:
       if os.path.exists( os.path.join(dirname,__file__) ):
           # The directory name for `__file__` was dirname
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my Python program: #!/usr/bin/env python import os BASE_PATH = os.path.dirname(__file__) print BASE_PATH
tests.py import unittest import sys import os sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))) import my_module.ext print sys.path print
I have this simple code here import os path = (raw_input(Enter dir: )) f
For example: import os print(os.listdir("path/to/dir")) will list files in a directory. How do I
import xlwt import xlrd import os.path import os print os.path.abspath(os.curdir) #Create workbook and worksheet
import os listing = os.listdir(path) for infile in listing: print infile f = open(os.path.join(path,
from sys import argv from os.path import exists script, from_file, to_file = argv print
I have this code: # File: zipfile-example-1.py import zipfile,os,glob file = zipfile.ZipFile(Apap.zip, w) #
Is there a better way to print __file__ without the extension? import os print
Assuming I have two Python modules and path_b is in the import path: #

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.