Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] use external files with inline coding

edited October 2011 in OpenSesame
Hi Sebastiaan,

I'm trying to open some text files in the inline coding, so far I have the files in the file pool but I don't know what is the command to open the files. Can you help me with that?

Thanks

Comments

  • edited 10:14PM

    Sure, you can use the get_file() function. The file pool is located somewhere in a temporary folder. get_file() retrieves the full path to a file in the file pool.

    So something like this should do the trick:

    path = self.experiment.get_file("recording.tsv")
    contents = open(path).read()
    print contents

    More info:
    http://osdoc.cogsci.nl/python-inline-code/experiment-functions#experiment.get_file

    Hope this helps!

  • edited 10:14PM

    Thanks!
    I originally tested my code in python and it was working fine, however, now using your suggestion the structure of the file (line by line) seemed to change.
    Before i had:
    path1 = self.experiment.get_file("design1.txt")
    design1 = open(path1, 'r', 0).read()
    for line in files:
    theline = line.strip().split(' ')
    theline[2] = float(theline[2])
    theline[2] = round(theline[2],1)
    theline[2] = theline[2] * 1000
    designlist[i].append(theline)

    and it worked. Now the content of "line" is just the first item of the first line, instead of the whole line. How can I change the code so that it works?

  • edited 10:14PM

    I don't completely follow, but if you want to walk through a file line by line, something like the following should work (in your example the variable 'files' appears to be undefined):

    path = self.experiment.get_file("my_file_name.txt")
    for line in open(path):
    do_something_with_line(line)

    Does this help? If not, could you describe in a bit more detail what you want to achieve, and what currently does not work?

    Good luck!

  • edited 10:14PM

    Thanks it worked!

Sign In or Register to comment.