Forum Discussion

stevereissps's avatar
stevereissps
Occasional Contributor
5 years ago
Solved

aqFile.Exists() path containing a space

Hello!

 

I am tring to use the aqFile functions to test for the exsistance of a file... When I send a file that contains a path with an embedded space, the return value is always FALSE.

 

Python : 

 

file_path_name = "C:\Path has Space\Reports\Report.pdf"

 

file_status = aqFile.Exists(file_path_name)  # check to see if file exists
file_status = aqFileSystem.Exists(file_path_name)

 

BOTH the above return FALSE

 

I have tried to put quotes around the "Path has Space" part without any luck...

 

Any suggestions would be greatly appreciated!!

 

Steve


  • Mr_Bro wrote:

    Can you please share the text which is retrieved from the hi object please

     

    Regards,

    Sathish


    Yeah, at his point, if hardcoding the string is working just fine, then the problem must be in what you're getting from the AUT.

8 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    I don't know if Python is the same as JavaScript... but backslashes may need to be doubled up.

     

    file_path_name = "C:\\Path has Space\\Reports\\Report.pdf"

     

    file_status = aqFile.Exists(file_path_name)  # check to see if file exists
    file_status = aqFileSystem.Exists(file_path_name)

    • stevereissps's avatar
      stevereissps
      Occasional Contributor

      Thanks for your reply!

       

      I have tried performing a search & replace on the path backslashes and that still is returning a False value.  If I hard code a string and try the aqFile.exists() on it - it works... :(       (see examples below)

       

      The problem arises when getting the path & filename from my info dialog.  Here is what is in the error log:

       

      Check_File_Status :: File Status NOT Correct : |C:\Users\username\Documents\Compass for SW\Jess-js3003_2019-09-27_11-07.pdf| : False : should be True

       

      Here is my function : 

       

      def Check_File_Exists(file_path_name, expected_status=True):
      # params : file_path_name : string = the file|path|drive to check
      # expected_status : boolean = the status the file should be

      file_status = aqFile.Exists(file_path_name) # check to see if file exists
      # file_status = aqFileSystem.Exists(file_path_name)

      retVal = file_status == expected_status # return the comparison status

      if not retVal:
      Log.Warning("Check_File_Status :: File Status NOT Correct : |%s| : %s : should be %s" % (file_path_name, file_status, expected_status))

      return retVal

       

      Running the hard-coded value works : 

       

      def Test1():
      the_file = 'C:\\Users\\username\\Documents\\Compass for SW\\Instrument_Reports\\Jess-js3003_2019-09-27_11-07.pdf'
      status = CommonUnit.Check_File_Exists(the_file, True)
      Log.Message("file status = %s" % status)

       

      Anything else you can suggest??

       

      Steve

  • Mr_Bro's avatar
    Mr_Bro
    Champion Level 0

    Hi stevereissps ,

     

    just make use of raw-string to over come for your problem.

    please find the below code snippet for your refernece and

    please let me know if that solved your problem or not.

    def testFileinFolder():
      file_path_name = r'C:\Users\sathishkumark\Desktop\Folder with space\report.pdf'
      file_status = aqFile.Exists(file_path_name)
      if file_status is True:
        Log.Checkpoint('File is present usder the path specified')
      else:
        Log.Error('File is Not present usder the path specified')

    Kind Regards,

    Sathish Kumar K

    • stevereissps's avatar
      stevereissps
      Occasional Contributor

      Thank you for your suggestions... I have tried a number of things to get this to work.

       

      If the file path is hard coded, it appears to work correctly.  If my script grabs the text from onscreen elements and puts them together (path + filename), it fails.  

       

      Here are the test cases that I have using : 

       

      Path text from on screen text elements (from JAVA GUI App) - Fails.  Using Python Raw string. : 

       

      1) Check_File_Status :: File Status NOT Correct : |C:\\Users\\steven.reiss\\Documents\\"Compass for SW"\\Instrument_Reports\\Jess-js3019_2019-10-01_13-10.pdf| : False : should be True

       

      2) Check_File_Status :: File Status NOT Correct : |C:\Users\steven.reiss\Documents\Compass for SW\Instrument_Reports\Jess-js3019_2019-10-01_13-11.pdf| : False : should be True

       

       

      Hard-Coded path - Works correctly

       

      1) 

       

      def Test1():
      the_file = 'C:\\Users\\steven.reiss\\Documents\\Compass for SW\\Instrument_Reports\\Jess-js3003_2019-10-01_11-24.pdf'
      val = aqObject.GetVarType(the_file)

      status = CommonUnit.Check_File_Exists(the_file, True)
      Log.Message("file status = %s %s" % (status, val))


      Result : file status = True    8  (varOleStr)

       

      2) 


      def testFileinFolder():
      # file_path_name = r'C:\Users\sathishkumark\Desktop\Folder with space\report.pdf'
      file_path_name = r'C:\Users\steven.reiss\Documents\Compass for SW\Instrument_Reports\Jess-js3003_2019-10-01_11-24.pdf'
      file_status = aqFile.Exists(file_path_name)
      Log.Message(aqObject.GetVarType(file_path_name))

      if file_status is True:
      Log.Checkpoint('File is present under the path specified')
      else:
      Log.Error('File is Not present under the path specified')

      status = CommonUnit.Check_File_Exists(file_path_name, True)
      Log.Message("Status = %s" % status)


      Result : Status = True

       

      Are there any other things you can suggest??

      • Mr_Bro's avatar
        Mr_Bro
        Champion Level 0

        Can you please share the text which is retrieved from the hi object please

         

        Regards,

        Sathish