2
$\begingroup$

Inside a Wolfram Script file, I'm trying to import another .wls file that's in a parent directory. More specifically, I'm trying to import common-methods.wls and common-methods-old.wls from atom-block.wls (file structure below). A problem occurs when I try to import both files, but not one.

Here's how my folder structure looks:

atom-colors
    atom-block.wls
    atom-classification.wls
common-methods.wls
common-methods-old.wls

As aforementioned, I try to import from atom-block.wls

#!/usr/bin/env wolframscript
(* ::Package:: *)

(* Sets current directory *)
x=FileNameSplit@$InputFileName;SetDirectory@FileNameJoin@Delete[x, Length@x];

Export["thing.txt", "content"];

Print@Directory[];
Import@FileNameJoin[{ParentDirectory[], "./common-methods-old.wls"}];
Print@Directory[];
Import@FileNameJoin[{ParentDirectory[], "./common-methods.wls"}];
Print@Directory[];

Export[FileNameJoin[{Directory[], "thing3.txt"}], "content"];

The fifth line gets the parent directory of atom-block, the file that is being executed. I did this so Directory[] yields the same result when executing via wolframscript -script ./atom-colors/atom-block.wls or wolframscript -script ./atom-block.wls.

The third print statement prints something strange. The console output is logged below.

C:\...\wolfram\atom-colors
C:\...\wolfram\atom-colors
C:\...\wolfram

I tried to find why the third print statement prints C:\...\wolfram (instead of C:\...\wolfram\atom-colors) while the second does not. The documentation says FileNameJoin[] joins names of file names, and does not actually change directories. Additionally, ParentDirectory[] only gives the parent of the current working directory. Besides, I would think the second and third print statements would be the same anyways.

Consequentially, the folder structure after running my program is the following:

atom-colors
    atom-block.wls
    atom-classification.wls
    thing.txt
common-methods.wls
common-methods-old.wls
thing3.txt

I understand I can use ResetDirectory[] to but a bandage over this issue, but the problem at hand really mystifies me and seems to be a much bigger concept that I don't understand. Any help is appreciated! Thanks, Edwin.

$\endgroup$

closed as off-topic by Kuba Jan 30 at 22:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Kuba
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ is SetDirectory @ DirectoryName @ $InputFileName what you are doing in the 5th line? $\endgroup$ – Kuba Jan 30 at 20:22
  • $\begingroup$ Don't we need to know what is inside common-methods.wls to be sure that it isn't changing Directory[]? $\endgroup$ – Kuba Jan 30 at 20:24
  • $\begingroup$ @Kuba Oh my gosh! Yes SetDirectory@DirectoryName@$InputFileName is exactly what I was going for. And you're right, common-methods.wls was changing the directory. I feel upset because I was stuck on this simple error for many days, trying to fix it the wrong way. Thanks for the help! $\endgroup$ – Edwin Jan 30 at 22:15
  • $\begingroup$ I am glad the problem is solved, good luck. $\endgroup$ – Kuba Jan 30 at 22:16

Browse other questions tagged or ask your own question.