What exactly is current working directory?
Your python interpreter location is based off of how you launched it, as well as subsequent actions taken after launching it like use of the os module to navigate your file system. Merely starting the interpreter will place you in the directory of your python installation (not the same on different operating systems). On the other hand, if you start by editing or running a file within a specific directory, your location will be the folder of the file you were editing. If you need to run the interpreter in a certain directory and you are using idle for example, it is easiest to start by creating a python file there one way or another and when you edit it you can start a shell with Run > Python Shell which will already be in that directory. If you are using the command line interpreter, navigate to the folder where you want to run your interpreter before running the python/python3/py command. If you need to navigate manually, you can of course use the following which has already been mentioned:
import os
os.chdir('full_path_to_your_directory')
How/where is the working directory of a program stored?
Current directory is maintained by the OS, not by language or framework. See description of GetCurrentDirectory WinAPI function for details.
From description:
Multithreaded applications and shared
library code should not use the
GetCurrentDirectory function and
should avoid using relative path
names. The current directory state
written by the SetCurrentDirectory
function is stored as a global
variable in each process, therefore
multithreaded applications cannot
reliably use this value without
possible data corruption from other
threads that may also be reading or
setting this value.
In what structure does Windows keep data about what is the working directory (or the current directory) of the process?
The shell (or the program that launches the script), calls the CreateProcess()
function. The CreateProcess function has the lpCurrentDirectory
parameter. The shell (or any other program that launches the script), sets the lpCurrentDirectory
parameter implicitly (~not through the parameters of the script).
That parameter is later stored in the undocumented property of the RTL_USER_PROCESS_PARAMETERS
structure. msdnLink, docLink
- note the word: undocumented (that's why I haven't found the info on msdn in the first place).
Related stackoverflow questions:
- Precisely what owns the 'current working directory'?
- How to set the working directory of a command in a Windows batch file?
- Difference between Current Directory and Working Directory in Windows
- How do I get the directory that a program is running from?
Current working directory
The problem, when I executed, was stat()
was failing.
This worked for me:
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
void listdir(const char* const name);
int main(void)
{
listdir(getenv("PWD"));
return 0;
}
void listdir(const char* const name)
{
DIR *dir;
struct dirent *entry;
struct stat buffer;
char* path = 0;
if (!(dir = opendir(name)))
return;
if (!(entry = readdir(dir)))
return;
do {
path =
malloc((strlen(name) + strlen(entry->d_name) + 2) * sizeof(char));
sprintf(path, "%s/%s", name, entry->d_name);
if (-1 == stat(path,&buffer))
{
fprintf(stderr, "stat(%s) failed: %s\n",
path, strerror(errno));
exit(1);
}
else if (S_ISDIR(buffer.st_mode))
{
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
{
listdir(path);
}
}
else
{
printf("%s\t%d\n", path, buffer.st_size);
}
free(path);
} while (entry = readdir(dir));
closedir(dir);
}
EDIT:
Removed call to chdir()
as realised it was superfluous. This does provide listing but does not do it using chdir()
.
Find the current directory and file's directory
To get the full path to the directory a Python file is contained in, write this in that file:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir()
to change your current working directory, since the value of the __file__
constant is relative to the current working directory and is not changed by an os.chdir()
call.)
To get the current working directory use
import os
cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The
os
andos.path
modules. - The
__file__
constant os.path.realpath(path)
(returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")os.path.dirname(path)
(returns "the directory name of pathnamepath
")os.getcwd()
(returns "a string representing the current working directory")os.chdir(path)
("change the current working directory topath
")
Shell script current directory?
The current(initial) directory of shell script is the directory from which you have called the script.
What is the current directory in a batch file?
From within your batch file:
%cd%
refers to the current working directory (variable)%~dp0
refers to the full path to the batch file's directory (static)%~dpnx0
and%~f0
both refer to the full path to the batch directory and file name (static).
See also: What does %~dp0 mean, and how does it work?
Related Topics
Python Dictionary Comprehension
How to Download Image Using Requests
Pass a List to a Function to Act as Multiple Arguments
Split String With Multiple Delimiters in Python
Add Scrolling to a Platformer in Pygame
Can't Modify List Elements in a Loop
Difference Between Images in 'P' and 'L' Mode in Pil
Deleting Dataframe Row in Pandas Based on Column Value
How to Find All Matches to a Regular Expression in Python
Changing the "Tick Frequency" on X or Y Axis in Matplotlib
Understanding the "Is" Operator
How to Capture Sigint in Python
How to Print a Single Backslash
How to Create a Text Input Box With Pygame
Adding a New Pandas Column With Mapped Value from a Dictionary