Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

If I run from the IDE the dir path is ok, but when I execute from termianl, I am geeting a exception:

Code:

def __init__(self):
        dir = os.path.abspath(os.path.join(__file__, os.pardir))
        self.LOCATOR_FILE_PATH = dir+'/locators/locator_'+self.get_class_name_lower()+'.json'
        with open(self.LOCATOR_FILE_PATH) as json_data:

Exception:

E
======================================================================
ERROR: test_verify_page (test_verify_page.TestVerifyPage)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_verify_page.py", line 9, in test_verify_page
    sign_inPage = SignInPage()
  File "C:\Python27\lib\site-packages\page_objects\sign_in_page.py", line 47, in __init__
    with open(self.LOCATOR_FILE_PATH) as json_data:
IOError: [Errno 2] No such file or directory: 'C:\\Python27\\lib\\site-packages\\page_objects/locators/locator_signinpage.json'

----------------------------------------------------------------------
Ran 1 test in 13.189s

FAILED (errors=1)

The real path where the file is placed is:

"C:\My_project\page_objects\locators\locator_signinpage.json'

I checked the settings in python console and I am not able to solve this.

share|improve this question
    
Why are you, on one line, using the os module to build a path correctly, then on the very next line using naive string concatenation, which doesn't understand, e.g., normalizing path separators? That's what os.path.join is for! –  Two-Bit Alchemist Mar 20 at 20:17
    
I am very new at Python, but I made the concatenation because the name of the JSON file that I should read deppends on a class name. (I am not sure if this answers your question) –  Juan Castelli Mar 20 at 20:21
    
My point was that your IOError raises with a path having both \\ (Windows file sep, escaped) and / (no idea if that works on Windows but standard everywhere else). You were right to build the first part of the path with os.path.join in line 2 of the code you posted, but line 3 should also use that and correcting that oversight will likely also stop you from having the kinds of problems like the one in your question. –  Two-Bit Alchemist Mar 20 at 20:26
    
Got it! Thanks @Two-BitAlchemist Now I am trying to build a good path using your tip. This could bue also a good practice??? dir = os.path.abspath(os.path.join(__file__, os.pardir)) f = '/locators/locator_'+self.get_class_name_lower()+'.json' dir2 = os.path.join(dir, f) –  Juan Castelli Mar 20 at 20:35
    
I am still having the issue from the terminal. Thanks for the tip aniway :D –  Juan Castelli Mar 20 at 20:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.