R command for setting working directory to source file location in Rstudio

To get the location of a script being sourced, you can use utils::getSrcDirectory or utils::getSrcFilename. So changing the working directory to that of the current file can be done with: setwd(getSrcDirectory()[1]) This does not work in RStudio if you Run the code rather than Sourceing it. For that, you need to use rstudioapi::getActiveDocumentContext. setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) This … Read more

python: Change the scripts working directory to the script’s own directory

This will change your current working directory to so that opening relative paths will work: import os os.chdir(“/home/udi/foo”) However, you asked how to change into whatever directory your Python script is located, even if you don’t know what directory that will be when you’re writing your script. To do this, you can use the os.path … Read more

How to find the working folder of a servlet based application in order to load resources

You could use ServletContext#getRealPath() to convert a relative web content path to an absolute disk file system path. String relativeWebPath = “/WEB-INF/static/file1.ext”; String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath); File file = new File(absoluteDiskPath); InputStream input = new FileInputStream(file); // … However, if your sole intent is to get an InputStream out of it, better use ServletContext#getResourceAsStream() instead … Read more

How to get the current directory in a C program?

Have you had a look at getcwd()? #include <unistd.h> char *getcwd(char *buf, size_t size); Simple example: #include <unistd.h> #include <stdio.h> #include <limits.h> int main() { char cwd[PATH_MAX]; if (getcwd(cwd, sizeof(cwd)) != NULL) { printf(“Current working dir: %s\n”, cwd); } else { perror(“getcwd() error”); return 1; } return 0; }