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.
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 whatos.path.join
is for! – Two-Bit Alchemist Mar 20 at 20:17\\
(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 withos.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:26dir = 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