This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
cntkillme's profile picture

fs.open/io.open creating files in root directory?

Started by cntkillme, 22 May 2017 - 04:01 AM
cntkillme #1
Posted 22 May 2017 - 06:01 AM
I have a script in /dir but when I create a file in it and run that script it creates the file in the root directory instead of in /dir.
I know I can use shell.resolve to solve this but why isn't this implicit?

Edit this seems to happen with every function that expects a path (i.e. os.run, etc.)
Edited on 22 May 2017 - 04:08 AM
Lyqyd #2
Posted 22 May 2017 - 06:39 AM
Because the APIs require absolute paths. The reason for this is that they don't have access to the shell "API" instance that your program is using. You could have multiple shells open alongside each other, and then which one would be the "right" one? Trying to poke up into the environment of the caller would be a very hacky and fragile thing to attempt, so it's better to just have the caller give the API calls the actual desired path instead of trying to guess it.
cntkillme #3
Posted 22 May 2017 - 07:58 AM
Oh yeah. For some reason I just assumed there was some global "working directory" since I totally forgot about multishell.
Sewbacca #4
Posted 01 June 2017 - 04:21 PM
I often use this function to resolve an file relative to the current directory. Note, the program must be called with shell.run to work correctly.

function locate (path)
  return fs.combine(fs.getParent(shell.getRunningProgram()), path)
end
This function should work, but there could be spelling mistakes (I wrote it down from my memories).