How to build a path within an existing directory

Asked by Ron Turrentine on 2018-08-06

Note: I am new to Python & SikuliX

I am looking at using SikuliX as an automation replacement for an existing script that I have in HPE UFT.
One of the things I am struggling with is how to create directory structures inside existing folders.

For example, let's say that I have a base folder called "C:\Screenshots" for the storing of screenshots from various applications.

In UFT, I have a VBScript function that checks to see if the full path exists - if it doesn't, it creates it:
(Note that the application name and the test type are determined elsewhere in the script on the fly)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
FullPath = "C:\Screenshots\MyApp\08-05-18\ProdTest"

Private Function BuildFullPath(ByVal FullPath)

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FolderExists(FullPath) Then

 BuildFullPath objFSO.GetParentFolderName(FullPath)
 objFSO.CreateFolder FullPath

End If

Set objFSO = Nothing

End Function
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

The above function will iterate through the folder structure, and if any portion of the path doesn't exist, it will create it.

I tried the below code in SIkuliX.
(The folder structure of "C:\Screenshots\Sikuli_Test" already exists.)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

import os

FullPath = "C:\Screenshots\Sikuli_Test\08-05-18\ProdTest"

os.mkdir(FullPath)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

SikuliX returned the following error:

OSError: (17, 'File exists', 'c:\\screenshots\\sikuli_test'

Part of the challenge is that the script will not always know how deep the given folder structure will already go. The script will need to determine this, and then create the missing folders as needed.

The VBScript function outlined above is simple & fast in its loop with the FileSystemObject. Is there a way to do something similarly elegant within Python & SikuliX?

Side question: can Python discern between a directory and a file? The error returned leads me to believe that it is confusing a an existing folder as an existing file.

Thanks in advance,

Ron Turrentine

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Ron Turrentine
Solved:
2018-08-06
Last query:
2018-08-06
Last reply:
2018-08-06
Ron Turrentine (oldtimerocker) said : #1

I seem to have found the answer!
The code that appears to be a good, working replacement is:

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

FullPath = "C:\Screenshots\Sikuli_Test\08-05-18\ProdTest"

myPath = os.path.dirname(FullPath)

if not os.path.exists(myPath):
        os.makedirs(myPath)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

I am going to keep this open, though, to see if anyone has any other suggestions. I would like to read them, as I am learning.

Thanks,

Ron

RaiMan (raimund-hocke) said : #2

This would be a function that does what your BuildFullPath() does:

def buildFullPath(base, new):
  target = os.path.join(base, new)
  try:
    os.makedirs(target)
    print "created:", target
  except:
    print "exists:", target

... and this would be a testscript:
def buildFullPath(base, new):
  target = os.path.join(base, new)
  try:
    os.makedirs(target)
    print "created:", target
  except:
    print "exists:", target

# the absolute path containig the script folder
basePath = getParentPath()

newFolder1 = "NewFolder1"
newSubFolder1 = "NewSubFolder1"
newPath1 = os.path.join(newFolder1, newSubFolder1)

for path in (newFolder1, newSubFolder1, newPath1):
  buildFullPath(basePath, path)

... just run it twice and you will see ;-)

Ron Turrentine (oldtimerocker) said : #3

Thank you RaiMan! I appreciate your helpful reply. I got it working!

I'm learning a great deal about both Python & Sikuli through the process.
I'm really enjoy it.

Best regards,

Ron