# Can I get the conditions file name/path in code component?

**URL:** <https://discourse.psychopy.org/t/can-i-get-the-conditions-file-name-path-in-code-component/1652>\
**Category:** Builder\
**Created:** [December 16, 2016, 10:32am UTC](https://discourse.psychopy.org/t/can-i-get-the-conditions-file-name-path-in-code-component/1652 "2016-12-16T10:32:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bpkarltoffel](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bpkarltoffel](https://discourse.psychopy.org/u/bpkarltoffel)\
**Post date:** [December 16, 2016, 10:32am UTC](https://discourse.psychopy.org/t/can-i-get-the-conditions-file-name-path-in-code-component/1652/1 "2016-12-16T10:32:42Z")

</div>

Hi there,

I would like to have the file name of the conditions file in a variable in a code component. I would like to write it into the trials data csv-file for logging purposes.  
I looked into the python code which is generated by PsychoPy and there I find it only hardcoded.

Cheers

---

<div class="post-metadata">

**Author:** ![jderrfuss](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jderrfuss/32/5245_2.png) [@jderrfuss](https://discourse.psychopy.org/u/jderrfuss)\
**Post date:** [December 16, 2016, 10:04pm UTC](https://discourse.psychopy.org/t/can-i-get-the-conditions-file-name-path-in-code-component/1652/2 "2016-12-16T22:04:46Z")

</div>

Hi,

If I haven’t misunderstood what you would like to achieve, you could create a code component like this:

```auto
inputFile="myInput.xlsx"
thisExp.addData("input", inputFile)

```

And then add $inputFile as conditions file in your loop.

Hope this helps.

Tom Ate

---

<div class="post-metadata">

**Author:** ![bpkarltoffel](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bpkarltoffel](https://discourse.psychopy.org/u/bpkarltoffel)\
**Post date:** [December 18, 2016, 4:09pm UTC](https://discourse.psychopy.org/t/can-i-get-the-conditions-file-name-path-in-code-component/1652/3 "2016-12-18T16:09:48Z")

</div>

Thanks,

yes that would work, but it’s not what I would really like to have. I am using the normal way in the builder to specifiy the conditions file and paste the path of the file in the loop parameters. So I have a hard coded conditions file name in the builder. What I would like to have is getting that file name into a code component. I would like to read it into a variable. I don’t know if that’s possible because in the python code generated by PsychoPy, the conditions file name is hard coded as well and I think it’s not saved in a variable - only the content of the conditionsfile is.

Cheers

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [December 18, 2016, 10:23pm UTC](https://discourse.psychopy.org/t/can-i-get-the-conditions-file-name-path-in-code-component/1652/4 "2016-12-18T22:23:44Z")

</div>

Yes, it looks like you are right: the TrialHandler object only seems to store the contents of the conditions file, not the name of the file itself.

One way around this is to make a column in your conditions file that actually contains the file name. It’s inefficient, as you’re specifying the name of the file multiple times (once per trial), but it does mean that the filename becomes available to you to use as a variable like any other.

---

<div class="post-metadata">

**Author:** ![bpkarltoffel](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bpkarltoffel](https://discourse.psychopy.org/u/bpkarltoffel)\
**Post date:** [December 19, 2016, 8:57am UTC](https://discourse.psychopy.org/t/can-i-get-the-conditions-file-name-path-in-code-component/1652/5 "2016-12-19T08:57:45Z")

</div>

Thank you Micheal,

this is actually not a bad idea. It looks strange in the conditions file and in the trials data but it’s the same with the session and partissipant number anyway.

Cheers

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [December 19, 2016, 8:38pm UTC](https://discourse.psychopy.org/t/can-i-get-the-conditions-file-name-path-in-code-component/1652/6 "2016-12-19T20:38:02Z")

</div>

Actually, there is a way to achieve this more directly.

Instead of putting the condition file name in the loop dialog, add a field to the experiment info dialog that appears before each experiment and insert the name of the conditions file there ( if required, add additional fields for other conditions files). For example, you could add a field called `loop_1_conditions` and put the .xlsx or .csv filename in there. Then in the loop dialog you can access that field’s content by putting this in its `Conditions` field:

```
$expInfo['loop_1_conditions']

```

You can also access the file name in a code component using the same expression (without the $ symbol).

---

<div class="post-metadata">

**Author:** ![bpkarltoffel](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bpkarltoffel](https://discourse.psychopy.org/u/bpkarltoffel)\
**Post date:** [December 20, 2016, 12:15pm UTC](https://discourse.psychopy.org/t/can-i-get-the-conditions-file-name-path-in-code-component/1652/7 "2016-12-20T12:15:46Z")

</div>

Hallo Michael,

interesting idea but for practicality reasons I will stick to the solution with the extra column in the conditions file. At the moment I have 32 psypy files and I just don’t want to fill in an extra field every time I run an experiment.

Cheers

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [December 20, 2016, 11:54pm UTC](https://discourse.psychopy.org/t/can-i-get-the-conditions-file-name-path-in-code-component/1652/8 "2016-12-20T23:54:34Z")

</div>

> [@bpkarltoffel](#):
>
> I have 32 psypy files

Really? I’m guessing you mean .psyexp files. If so, there really is quite an issue there that I’m sure could be resolved in a simpler way.
