How do I get the directory from a file's full path?
If you've definitely got an absolute path, use Path.GetDirectoryName(path)
.
If you might only get a relative name, use new FileInfo(path).Directory.FullName
.
Note that Path
and FileInfo
are both found in the namespace System.IO
.
How to get full path of a file?
Use readlink:
readlink -f file.txt
How to extract directory path from file path?
dirname
and basename
are the tools you're looking for for extracting path components:
$ VAR='/home/pax/file.c'
$ DIR="$(dirname "${VAR}")" ; FILE="$(basename "${VAR}")"
$ echo "[${DIR}] [${FILE}]"
[/home/pax] [file.c]
They're not internal bash
commands but they are part of the POSIX standard - see dirname
and basename
. Hence, they're probably available on, or can be obtained for, most platforms that are capable of running bash
.
How do I get the absolute directory of a file in Bash?
To get the full path use:
readlink -f relative/path/to/file
To get the directory of a file:
dirname relative/path/to/file
You can also combine the two:
dirname $(readlink -f relative/path/to/file)
If readlink -f
is not available on your system you can use this*:
function myreadlink() {
(
cd "$(dirname $1)" # or cd "${1%/*}"
echo "$PWD/$(basename $1)" # or echo "$PWD/${1##*/}"
)
}
Note that if you only need to move to a directory of a file specified as a relative path, you don't need to know the absolute path, a relative path is perfectly legal, so just use:
cd $(dirname relative/path/to/file)
if you wish to go back (while the script is running) to the original path, use pushd
instead of cd
, and popd
when you are done.
* While myreadlink
above is good enough in the context of this question, it has some limitation relative to the readlink
tool suggested above. For example it doesn't correctly follow a link to a file with different basename
.
How to get the directory of a file from the full path in C
What you're looking for is dirname(3)
. This is POSIX-only.
A Windows alternative would be _splitpath_s
.
errno_t _splitpath_s(
const char * path,
char * drive,
size_t driveNumberOfElements,
char * dir,
size_t dirNumberOfElements,
char * fname,
size_t nameNumberOfElements,
char * ext,
size_t extNumberOfElements
);
Sample code (untested):
#include <stdlib.h>
const char* path = "C:\\some\\dir\\file";
char dir[256];
_splitpath_s(path,
NULL, 0, // Don't need drive
dir, sizeof(dir), // Just the directory
NULL, 0, // Don't need filename
NULL, 0);
How can I extract the folder path from file path in Python?
You were almost there with your use of the split
function. You just needed to join the strings, like follows.
>>> import os
>>> '\\'.join(existGDBPath.split('\\')[0:-1])
'T:\\Data\\DBDesign'
Although, I would recommend using the os.path.dirname
function to do this, you just need to pass the string, and it'll do the work for you. Since, you seem to be on windows, consider using the abspath
function too. An example:
>>> import os
>>> os.path.dirname(os.path.abspath(existGDBPath))
'T:\\Data\\DBDesign'
If you want both the file name and the directory path after being split, you can use the os.path.split
function which returns a tuple, as follows.
>>> import os
>>> os.path.split(os.path.abspath(existGDBPath))
('T:\\Data\\DBDesign', 'DBDesign_93_v141b.mdb')
How to obtain the absolute path of a file via Shell (BASH/ZSH/SH)?
Use realpath
$ realpath example.txt
/home/username/example.txt
How do you get full paths of all files in a directory in Go?
If you want to see a full path, you should start with a full path. .
is a relative path.
You can get the working path with os.Getwd
path, err := os.Getwd()
// handle err
printFiles(path)
The rest is simply appending the file name to the directory path. You should use the path/filepath
package for that:
for _, file := range fileInfos {
fmt.Println(filepath.Join(path, file.Name())
}
Related Topics
Cannot Access Non-Static Field
.Net Asynchronous Stream Read/Write
Check If Property Has Attribute
Getting Absolute Urls Using ASP.NET Core
What Is the Correct Way to Read a Serial Port Using .Net Framework
How to Hide "Chrome Is Being Controlled by Automated Software" Infobar Within Chrome V76
Create Datatemplate in Codebehind
Datatrigger Does Not Change Text Property
Checking If an Object Is Null in C#
ASP.NET Webapi2 Enable Cors Not Working with Aspnet.Webapi.Cors 5.2.3
What Does the Keyword "New" Do to a Struct in C#
I Need a Fast Runtime Expression Parser
ASP.NET 2012 Unobtrusive Validation with Jquery
Anonymous Types - Are There Any Distingushing Characteristics