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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:15:20+00:00 2026-05-15T12:15:20+00:00

This is my first question. My python script opens and reads from a present

  • 0

This is my first question.

My python script opens and reads from a present text file using the following simple funct:

open("config.ini", "r")

As this is a relative path it is supposed to work because config.ini is placed in the same directory like the script is when it is launched, that should be the current working dir.

In fact this works perfectly on all of my 3 linux boxes, but I have one user who demands support because he gets an error while opening config.ini. The error raises because

os.path.exists("config.ini")

returns false even if the file is there!

Trying to fix this problem we found out that the only way to make it work is to place config.ini in his home directory despite the supposed working directory is another.

Also, if my script tries to create a file in the present working directory, the file is always created in his home dir instead, and so I think that for some reason his working dir is always home!

How can I troubleshoot this problem? Maybe I could introduce absolute paths, but I am afraid that os.getcwd() would return the homedir instead of the correct one.

Should I maybe suggest this user to fix his machine in some way?

Sorry for this long question but english is not my first language and I am a beginner in coding, so have some difficulties to explain.

Thank you very much 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-15T12:15:20+00:00Added an answer on May 15, 2026 at 12:15 pm

    Could it be that the user is executing your script from his home directory?

    I.e. suppose the script is in:

    /home/user/test/foo/foo.py
    

    But the user calls it thus:

    /home/user> python test/foo/foo.py
    

    In this case, the “current directory” the script sees is /home/user.

    What you can do is find out the directory the script itself resides in by calling this function:

    import os
    
    def script_dir():
        return os.path.dirname(os.path.realpath(__file__))
    

    It will always return the directory in which the script lives, not the current directory which may be different. You can then store your configuration file there safely.

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

Sidebar

Related Questions

I'm using the first answer to this question Overcoming Python's limitations regarding instance methods
Using: Ubuntu 11.04 Django 1.3 Python 2.7 Following the tutorial at Writing your first
this is my first question in here, and I would like to ask if
This is my first question on Stack Overflow, so I hope that I'm clear
This is my first question, so please be gentle. I'm trying out Apache Camel
this is my first question to SO so i'll try not to disgrace myself.
this is my first question I have searched for any launching activity questions and
this is my first question here so be gentle ! recently i try to
this is my first question on stack overflow. Some quick background, this is not
This is my first question, but I've already found this site extremely helpful, so

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.