Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

How can I retrieve/use the project location in a relative path to open and read/write to a text file

tcammans
tcammans Posts: 3
edited September 2018 in SOAtest

I am using the following Jython script to read and then update a value from a text file on my local pc, but need to be able to use a relative path to identify the file in both line 7 and 14. I have verified my PROJECT_LOC variable is: "C:/Users//File/Path/To/Workspace/Project". Any help is appreciated.

Works Locally but not on Server:
from java.util import *
from java.text import *
from java.io.File import *
import cStringIO

def writeString():
outwrite = open("C:/Users//File/Path/To/Workspace/Project/File.txt","r+")
OrderNoStr = outwrite.readline()
print OrderNoStr
OrderNoInt = int(OrderNoStr)
print OrderNoInt
NewOrderNoInt = OrderNoInt + 1
print NewOrderNoInt
outwrite = open("C:/Users//File/Path/To/Workspace/Project/File.txt","w")
outwrite.write(str(NewOrderNoInt))
outwrite.close()

Best Answer

Answers

  • Thomas Moore
    Thomas Moore Posts: 82 admin
    edited September 2018

    Hi tcammans,

    The script itself looks correct. The one question I have is related to the server portion though: what OS is your server running on? Looking at the script, it looks like the path to your workspace is hardcoded. If this is the case and the server is running on Linux architecture, the script will fail as it is trying to open a file given by a windows path.

    If it is on a Windows server, is the users directory in your path hardcoded to an incorrect/nonexistent directory? If this is the case, then that could also cause the script to fail.


    Unless someone else on here can come up with an easier means of grabbing this info through a script, one thing that you could to do dynamically grab this value is have a messaging client set to the None with ${project_loc} in the request body. Then chain a text databank to the request and extract the location.

    From your script you can then grab the value using context.getValue("Generated Data Source", "your_extraction_name_here")


    EDIT: I would highly recommend going with Nathan's solution in the post below mine, much simpler without as many moving parts :).

    Hope this helps!

  • tcammans
    tcammans Posts: 3

    Thanks @jakubiak and @Thomas Moore I was able to get it working with your help. What the working script looks like now is:

    from com.parasoft.api import *
    from java.util import *
    from java.text import *
    from java.io.File import *
    import cStringIO

    def writeString(input, context):

    outwrite = open(str(context.getAbsolutePathFile("File.txt")),"r+")
    OrderNoStr = outwrite.readline()
    print OrderNoStr
    OrderNoInt = int(OrderNoStr)
    print OrderNoInt
    NewOrderNoInt = OrderNoInt + 1
    print NewOrderNoInt
    outwrite = open(str(context.getAbsolutePathFile("File.txt")),"w")
    outwrite.write(str(NewOrderNoInt))
    outwrite.close()
    

    I had not realized that I needed to make the changes on the Input Tab to use a File input and establish the file to use as File.txt (in this example).

    Thanks again.