I have the following issue using Path from the pathlib library:
from pathlib import Path
import os
path_list = ["path","to","directory1","directory2"]
join_str = os.path.sep
my_path = join_str.join(path_list)
my_path = str(Path(my_path))
dir_exists = os.path.isdir(my_path)
Output:
my_path = "path/to/directory1/directory2"
dir_exists = False
I have tried with Path itself and manually giving it a path that I know exists:
(Pdb) test2 = str(Path("Users","userK","my_directory"))
(Pdb) os.path.isdir(test2)
False
(Pdb) test2
Users/userK/my_directory
I was wondering why Path doesn't simply add the backslash at the beginning? Is there a better way to build my path with the needed path separator from a list?
I didn't notice it before, because I use os.getcwd() and add to it to build my paths:
(Pdb) my_cwd = os.getcwd()
(Pdb) my_path = str(Path(my_cwd,"directory1"))
(Pdb) os.path.isdir(my_path)
True
However, I am not in the position code-wise to explicitly build the path I need, hence the issue.
Thanks!
Path cannot assume how the result will be used further, so it constructs the path object from the input data as is.
I will demonstrate with examples.
To begin with, prepare the directories in /tmp:
zyzop#localhost:~$ cd /tmp/
zyzop#localhost:/tmp$ pwd
/tmp
zyzop#localhost:/tmp$ mkdir -p path/to/directory1/directory2
zyzop#localhost:/tmp$ ls -ld path/to/directory1/directory2
drwxr-xr-x 2 zyzop zyzop 4096 Jul 8 03:28 path/to/directory1/directory2
zyzop#localhost:/tmp$ python3
Let's create a relative path from the list of its parts
>>> from pathlib import Path
>>> path_list = ["path", "to", "directory1", "directory2"]
>>> my_path = Path(*path_list)
>>> my_path
PosixPath('path/to/directory1/directory2')
Since the relative path exists in the current directory, we can check its existence
>>> my_path.exists()
True
>>> my_path.is_absolute()
False
>>> my_path.resolve()
PosixPath('/tmp/path/to/directory1/directory2')
There are two ways to create an absolute path. The first is to attach the path to the current directory to the beginning of the path
>>> my_path1 = Path(Path().cwd(), *path_list)
>>> my_path1
PosixPath('/tmp/path/to/directory1/directory2')
>>> my_path1.is_absolute()
True
>>> my_path1.exists()
True
The second way is to explicitly specify the path from the root
>>> path_list2 = ["/", "tmp", "path", "to", "directory1", "directory2"]
>>> my_path2 = Path(*path_list2)
>>> my_path2
PosixPath('/tmp/path/to/directory1/directory2')
>>> my_path2.exists()
True
>>> my_path2.is_absolute()
True
For example, let's see what happens to relative paths in another directory. Let's move to the home directory
>>> import os
>>> os.chdir(Path.home())
>>> Path().cwd()
PosixPath('/home/zyzop')
Let's check the existence of paths
>>> my_path
PosixPath('path/to/directory1/directory2')
>>> my_path.exists()
False
>>> my_path2
PosixPath('/tmp/path/to/directory1/directory2')
>>> my_path2.exists()
True
Now let's see how the absolute paths have changed. Since my_path is a relative path, it will always resolve relative to the current directory
>>> my_path.resolve()
PosixPath('/home/zyzop/path/to/directory1/directory2')
my_path 2 is an absolute path, so it will always be unchanged
>>> my_path2.resolve()
PosixPath('/tmp/path/to/directory1/directory2')
More details can be found here: https://docs.python.org/3/library/pathlib.html
I do not know why pathlib does not add a slash at the beginning. If I had to guess, maybe sometimes it is not obvious what is the "root" of the file system (think windows with multiple disks) and also it is probably better this way.
Anyway, easy to add it yourself.
Path("/path/to/directory1/directory2")
Path("/Users/userK/my_directory")
Path("./directory1") # or Path.cwd() / "directory1"
To get the root, you can use this syntax :
from pathlib import Path
Path.cwd().root
Another approach could be :
from pathlib import Path
Path("/") # Or Path("/").root
It works on UNIX, to test on Windows
I have this simple Python code which supposed to return files in path. But it returns an empty list all the time. Why ?
My array path is correct.
import glob
flowers_path = 'C:/flower_v1/training-images/*/*.jpg'
addrs = glob.glob(flowers_path)
print(addrs)
Returns :
[]
My directory structure :
I am doing the execution on Google Collab.
You aren't quite getting the glob syntax right. You should try this:
import glob
flowers_path = 'C:/flower_v1/training-images/**/*.jpg' # ** is for going through directories
address = glob.glob(flowers_path, recursive=True) # recursive flag must be true
print(address)
or simply
address = glob.glob('/**/*.jpg',recursive=True)
I know there are functions for finding parent directory or path such as.
os.path.dirname(os.path.realpath(__file__))
'C:\Users\jahon\Desktop\Projects\CAA\Result\caa\project_folder'
Is there a function that just returns the parent folder name? In this case it should be project_folder.
You can achieve this easily with os
import os
os.path.basename(os.getcwd())
You can get the last part of any path using basename (from os.path):
>>> from os.path import basename
>>> basename('/path/to/directory')
'directory'
Just to note, if your path ends with / then the last part of the path is empty:
>>> basename('/path/to/directory/')
''
Yes, you can use PurePath.
PurePath(__file__).parent.name == 'parent_dir'
You can use split and os.path.sep to get the list of path elements and then call the last element of the list:
import os
path = 'C:\\Users\\jahon\\Desktop\\Projects\\CAA\\Result\\caa\\project_folder'
if path.split(os.path.sep)[-1]:
parent_folder = path.split(os.path.sep)[-1] # if no backslashes at the end
else:
parent_folder = path.split(os.path.sep)[-2] # with backslashes at the end
The following code in Python gives me the current path.
import os
DIR = os.path.dirname(os.path.dirname(__file__))
How can I now use the variable DIR to go down one more directory? I don't want to change the value of DIR as it is used elsewhere.
I have tried this:
DIR + "../path/"
But it does not seems to work.
Call one more dirname:
os.path.join(os.path.dirname(DIR), 'path')
Try:
import os.path
print(os.path.abspath(os.path.join(DIR, os.pardir)))
When you join a path via '+' you have to add a 'r':
path = r'C:/home/' + r'user/dekstop'
or write double backslashes:
path = 'C://home//' + 'user//dekstop'
Anyway you should never use that!
That's the best way:
import os
path = os.path.join('C:/home/', 'user/dekstop')
Could someone tell me how to get the parent directory of a path in Python in a cross platform way. E.g.
C:\Program Files ---> C:\
and
C:\ ---> C:\
If the directory doesn't have a parent directory, it returns the directory itself. The question might seem simple but I couldn't dig it up through Google.
Python 3.4
Use the pathlib module.
from pathlib import Path
path = Path("/here/your/path/file.txt")
print(path.parent.absolute())
Old answer
Try this:
import os
print os.path.abspath(os.path.join(yourpath, os.pardir))
where yourpath is the path you want the parent for.
Using os.path.dirname:
>>> os.path.dirname(r'C:\Program Files')
'C:\\'
>>> os.path.dirname('C:\\')
'C:\\'
>>>
Caveat: os.path.dirname() gives different results depending on whether a trailing slash is included in the path. This may or may not be the semantics you want. Cf. #kender's answer using os.path.join(yourpath, os.pardir).
The Pathlib method (Python 3.4+)
from pathlib import Path
Path('C:\Program Files').parent
# Returns a Pathlib object
The traditional method
import os.path
os.path.dirname('C:\Program Files')
# Returns a string
Which method should I use?
Use the traditional method if:
You are worried about existing code generating errors if it were to use a Pathlib object. (Since Pathlib objects cannot be concatenated with strings.)
Your Python version is less than 3.4.
You need a string, and you received a string. Say for example you have a string representing a filepath, and you want to get the parent directory so you can put it in a JSON string. It would be kind of silly to convert to a Pathlib object and back again for that.
If none of the above apply, use Pathlib.
What is Pathlib?
If you don't know what Pathlib is, the Pathlib module is a terrific module that makes working with files even easier for you. Most if not all of the built in Python modules that work with files will accept both Pathlib objects and strings. I've highlighted below a couple of examples from the Pathlib documentation that showcase some of the neat things you can do with Pathlib.
Navigating inside a directory tree:
>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
Querying path properties:
>>> q.exists()
True
>>> q.is_dir()
False
import os
p = os.path.abspath('..')
C:\Program Files ---> C:\\\
C:\ ---> C:\\\
An alternate solution of #kender
import os
os.path.dirname(os.path.normpath(yourpath))
where yourpath is the path you want the parent for.
But this solution is not perfect, since it will not handle the case where yourpath is an empty string, or a dot.
This other solution will handle more nicely this corner case:
import os
os.path.normpath(os.path.join(yourpath, os.pardir))
Here the outputs for every case that can find (Input path is relative):
os.path.dirname(os.path.normpath('a/b/')) => 'a'
os.path.normpath(os.path.join('a/b/', os.pardir)) => 'a'
os.path.dirname(os.path.normpath('a/b')) => 'a'
os.path.normpath(os.path.join('a/b', os.pardir)) => 'a'
os.path.dirname(os.path.normpath('a/')) => ''
os.path.normpath(os.path.join('a/', os.pardir)) => '.'
os.path.dirname(os.path.normpath('a')) => ''
os.path.normpath(os.path.join('a', os.pardir)) => '.'
os.path.dirname(os.path.normpath('.')) => ''
os.path.normpath(os.path.join('.', os.pardir)) => '..'
os.path.dirname(os.path.normpath('')) => ''
os.path.normpath(os.path.join('', os.pardir)) => '..'
os.path.dirname(os.path.normpath('..')) => ''
os.path.normpath(os.path.join('..', os.pardir)) => '../..'
Input path is absolute (Linux path):
os.path.dirname(os.path.normpath('/a/b')) => '/a'
os.path.normpath(os.path.join('/a/b', os.pardir)) => '/a'
os.path.dirname(os.path.normpath('/a')) => '/'
os.path.normpath(os.path.join('/a', os.pardir)) => '/'
os.path.dirname(os.path.normpath('/')) => '/'
os.path.normpath(os.path.join('/', os.pardir)) => '/'
os.path.split(os.path.abspath(mydir))[0]
os.path.abspath(os.path.join(somepath, '..'))
Observe:
import posixpath
import ntpath
print ntpath.abspath(ntpath.join('C:\\', '..'))
print ntpath.abspath(ntpath.join('C:\\foo', '..'))
print posixpath.abspath(posixpath.join('/', '..'))
print posixpath.abspath(posixpath.join('/home', '..'))
import os
print"------------------------------------------------------------"
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
print("example 1: "+SITE_ROOT)
PARENT_ROOT=os.path.abspath(os.path.join(SITE_ROOT, os.pardir))
print("example 2: "+PARENT_ROOT)
GRANDPAPA_ROOT=os.path.abspath(os.path.join(PARENT_ROOT, os.pardir))
print("example 3: "+GRANDPAPA_ROOT)
print "------------------------------------------------------------"
>>> import os
>>> os.path.basename(os.path.dirname(<your_path>))
For example in Ubuntu:
>>> my_path = '/home/user/documents'
>>> os.path.basename(os.path.dirname(my_path))
# Output: 'user'
For example in Windows:
>>> my_path = 'C:\WINDOWS\system32'
>>> os.path.basename(os.path.dirname(my_path))
# Output: 'WINDOWS'
Both examples tried in Python 2.7
Suppose we have directory structure like
1]
/home/User/P/Q/R
We want to access the path of "P" from the directory R then we can access using
ROOT = os.path.abspath(os.path.join("..", os.pardir));
2]
/home/User/P/Q/R
We want to access the path of "Q" directory from the directory R then we can access using
ROOT = os.path.abspath(os.path.join(".", os.pardir));
If you want only the name of the folder that is the immediate parent of the file provided as an argument and not the absolute path to that file:
os.path.split(os.path.dirname(currentDir))[1]
i.e. with a currentDir value of /home/user/path/to/myfile/file.ext
The above command will return:
myfile
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
parent_path = os.path.abspath(os.path.join(dir_path, os.pardir))
import os.path
os.path.abspath(os.pardir)
Just adding something to the Tung's answer (you need to use rstrip('/') to be more of the safer side if you're on a unix box).
>>> input1 = "../data/replies/"
>>> os.path.dirname(input1.rstrip('/'))
'../data'
>>> input1 = "../data/replies"
>>> os.path.dirname(input1.rstrip('/'))
'../data'
But, if you don't use rstrip('/'), given your input is
>>> input1 = "../data/replies/"
would output,
>>> os.path.dirname(input1)
'../data/replies'
which is probably not what you're looking at as you want both "../data/replies/" and "../data/replies" to behave the same way.
print os.path.abspath(os.path.join(os.getcwd(), os.path.pardir))
You can use this to get the parent directory of the current location of your py file.
GET Parent Directory Path and make New directory (name new_dir)
Get Parent Directory Path
os.path.abspath('..')
os.pardir
Example 1
import os
print os.makedirs(os.path.join(os.path.dirname(__file__), os.pardir, 'new_dir'))
Example 2
import os
print os.makedirs(os.path.join(os.path.dirname(__file__), os.path.abspath('..'), 'new_dir'))
os.path.abspath('D:\Dir1\Dir2\..')
>>> 'D:\Dir1'
So a .. helps
import os
def parent_filedir(n):
return parent_filedir_iter(n, os.path.dirname(__file__))
def parent_filedir_iter(n, path):
n = int(n)
if n <= 1:
return path
return parent_filedir_iter(n - 1, os.path.dirname(path))
test_dir = os.path.abspath(parent_filedir(2))
The answers given above are all perfectly fine for going up one or two directory levels, but they may get a bit cumbersome if one needs to traverse the directory tree by many levels (say, 5 or 10). This can be done concisely by joining a list of N os.pardirs in os.path.join. Example:
import os
# Create list of ".." times 5
upup = [os.pardir]*5
# Extract list as arguments of join()
go_upup = os.path.join(*upup)
# Get abspath for current file
up_dir = os.path.abspath(os.path.join(__file__, go_upup))
To find the parent of the current working directory:
import pathlib
pathlib.Path().resolve().parent
import os
def parent_directory():
# Create a relative path to the parent of the current working directory
relative_parent = os.path.join(os.getcwd(), "..") # .. means parent directory
# Return the absolute path of the parent directory
return os.path.abspath(relative_parent)
print(parent_directory())