What is a cross-platform way to get the current directory?
If it is no problem for you to include, use boost filesystem for convenient cross-platform filesystem operations.
boost::filesystem::path full_path( boost::filesystem::current_path() );
Here is an example.
EDIT: as pointed out by Roi Danton in the comments, filesystem became part of the ISO C++ in C++17, so boost is not needed anymore:
std::filesystem::current_path();
What is a more crossplatform way of opening Explorer/file manager shell with the current directory?
On OSX an easy way to do this is to use open
:
import subprocess
subprocess.call(['/usr/bin/open', '~'])
How to get the current directory in python?
You can just replace the \
with /
. Note that the former must be escaped with another \
, like below:
import os
directory = os.getcwd().replace('\\', '/')
How do I get the current directory using C# that my Windows App Store app is running in?
AppDomain.CurrentDomain.BaseDirectory
works and is cross-platform - included in .NET Standard.
What is the cross-platform way of obtaining the path to the local application data directory?
You could probably say something like (contradict me if I am wrong, or if this a bad approach)
private String workingDirectory;
//here, we assign the name of the OS, according to Java, to a variable...
private String OS = (System.getProperty("os.name")).toUpperCase();
//to determine what the workingDirectory is.
//if it is some version of Windows
if (OS.contains("WIN"))
{
//it is simply the location of the "AppData" folder
workingDirectory = System.getenv("AppData");
}
//Otherwise, we assume Linux or Mac
else
{
//in either case, we would start in the user's home directory
workingDirectory = System.getProperty("user.home");
//if we are on a Mac, we are not done, we look for "Application Support"
workingDirectory += "/Library/Application Support";
}
//we are now free to set the workingDirectory to the subdirectory that is our
//folder.
Note that, in this code, I am taking full advantage that Java treats '/'
the same as '\\'
when dealing with directories. Windows uses '\\'
as pathSeparator, but it is happy with '/'
, too. (At least Windows 7 is.) It is also case-insensitive on it's environment variables; we could have just as easily said workingDirectory = System.getenv("APPDATA");
and it would have worked just as well.
how to show current directory in ipython prompt
Assuming you're interested in configuring this for all subsequent invocations of ipython, run the following (in a traditional shell, like bash :) ). It appends to your default ipython configuration, creating it if necessary. The last line of the configuration file will also automatically make all the executables in your $PATH available to simply run in python, which you probably also want if you're asking about cwd in the prompt. So you can run them without a ! prefix. Tested with IPython 7.18.1.
mkdir -p ~/.ipython/profile_default
cat >> ~/.ipython/profile_default/ipython_config.py <<EOF
from IPython.terminal.prompts import Prompts, Token
import os
class MyPrompt(Prompts):
def cwd(self):
cwd = os.getcwd()
if cwd.startswith(os.environ['HOME']):
cwd = cwd.replace(os.environ['HOME'], '~')
cwd_list = cwd.split('/')
for i,v in enumerate(cwd_list):
if i not in (1,len(cwd_list)-1): #not last and first after ~
cwd_list[i] = cwd_list[i][0] #abbreviate
cwd = '/'.join(cwd_list)
return cwd
def in_prompt_tokens(self, cli=None):
return [
(Token.Prompt, 'In ['),
(Token.PromptNum, str(self.shell.execution_count)),
(Token.Prompt, '] '),
(Token, self.cwd()),
(Token.Prompt, ': ')]
c.TerminalInteractiveShell.prompts_class = MyPrompt
c.InteractiveShellApp.exec_lines = ['%rehashx']
EOF
(c.PromptManager only works in older versions of ipython.)
Related Topics
Tool to Generate Xml File from Xsd (For Testing)
Qtextedit with Different Text Colors (Qt/C++)
How to Resize an Image to a Specific Size in Opencv
Using Std::Variant with Recursion, Without Using Boost::Recursive_Wrapper
C++ Regular Expressions with Boost Regex
What Is the Fastest Way to Compute Large Power of 2 Modulo a Number
How to Get a List Video Capture Devices Names (Web Cameras) Using Qt (Crossplatform)? (C++)
Floating Point to Binary Value(C++)
Cost of Using Std::Map with Std::String Keys VS Int Keys
Using Raii to Manage Resources from a C-Style API
Good C++ Array Class for Dealing with Large Arrays of Data in a Fast and Memory Efficient Way
How to Wrap Functions with the '--Wrap' Option Correctly
Stopping an Infinite Loop in C++ When Key Is Pressed
How to Turn on Multi-Cpu/Core C++ Compiles in the Visual Studio Ide (2008)
C++ Is It Necessary to Delete Dynamically Allocated Objects at the End of the Main Scope
a Few Things About Division by Zero in C
What's the Difference Between Cout<<Cout and Cout<<&Cout in C++