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 7697953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:07:46+00:00 2026-05-31T22:07:46+00:00

I have following code: os.chdir(os.path.dirname(os.path.realpath(__file__)) + /../test) path.append(os.getcwd()) os.chdir(os.path.dirname(os.path.realpath(__file__))) Which should add /../test to

  • 0

I have following code:

os.chdir(os.path.dirname(os.path.realpath(__file__)) + "/../test")
path.append(os.getcwd())
os.chdir(os.path.dirname(os.path.realpath(__file__)))

Which should add /../test to python path, and it does so, and it all runs smoothly afterwards on eclipse using PyDev.

But when I lunch same app from console second os.chdir does something wrong, actually the wrong thing is in os.path.realpath(__file__) cus it returns ../test/myFile.py in stead of ../originalFolder/myFile.py. Of course I can fix this by using fixed os.chdir("../originalFolder") but that seems a bit wrong to me, but this works on both, eclipse and console.

P.S. I’m using os.getcwd() actually because I want to make sure there isn’t such folder already added, otherwise I wouldn’t have to switch dir’s at all

So is there anything wrong with my approach or I have messed something up? or what? 🙂

Thanks in advance! 🙂

  • 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-31T22:07:48+00:00Added an answer on May 31, 2026 at 10:07 pm

    Take a look what is value of __file__. It doesn’t contain absolute path to your script, it’s a value from command line, so it may be something like “./myFile.py” or “myFile.py”. Also, realpath() doesn’t make path absolute, so realpath(“myFile.py”) called in different directory will still return “myFile.py”.

    I think you should do ssomething like this:

    import os.path
    
    script_dir = os.path.dirname(os.path.abspath(__file__))
    target_dir = os.path.join(script_dir, '..', 'test')
    print(os.getcwd())
    os.chdir(target_dir)
    print(os.getcwd())
    os.chdir(script_dir)
    print(os.getcwd())
    

    On my computer (Windows) I have result like that:

    e:\parser>c:\Python27\python.exe .\rp.py
    e:\parser
    e:\test
    e:\parser
    
    e:\parser>c:\Python27\python.exe ..\parser\rp.py
    e:\parser
    e:\test
    e:\parser
    

    Note: If you care for compatibility (you don’t like strange path errors) you should use os.path.join() whenever you combine paths.

    Note: I know my solution is dead simple (remember absolute path), but sometimes simplest solutions are best.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code which add BackButton on my view's navigation item's tabbar. It
I have following code: VAR1= ANOTHER_VAR=$VAR1/path/to/file ANOTHER_VAR_2=$VAR1/path/to/another/file ... # getopts which reads params from
I have following Code Block Which I tried to optimize in the Optimized section
I have following code to find if row is selected, which is selected and
I have the following code: Dir.chdir(mydir) Dir.entries(mydir).each do |file| log.info(file) end My problem is
I have the following code: import os import csv listing = os.listdir('/directory/my/files/are/in') os.chdir('/directory/my/files/are/in') for
I have following code which works fine in iPhone application but in iPad I
i have following code in which, i am fetching the data from the sqlite
I have following code I want to test: public class MessageService { private MessageDAO
I have the following code which works fine, but it doesn't send the attachment

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.