Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check absolute paths in Python

Tags:

python

path

How can I check whether two file paths point to the same file in Python?

like image 224
lfaraone Avatar asked Jul 30 '09 17:07

lfaraone


People also ask

How do you find the absolute path in Python?

Use abspath() to Get the Absolute Path in Python To get the absolute path using this module, call path. abspath() with the given path to get the absolute path. The output of the abspath() function will return a string value of the absolute path relative to the current working directory.

How do I know if my path is absolute?

You can determine the absolute path of any file in Windows by right-clicking a file and then clicking Properties. In the file properties first look at the "Location:" which is the path to the file.

How do I get the full path of a file in Python?

To get current file's full path, you can use the os. path. abspath function. If you want only the directory path, you can call os.

How do you find relative paths in Python?

First, you have to import the os module in Python so you can run operating system functionalities in your code. Then you create the variable absolute_path which fetches the current directory relative to the root folder. This is the full path to your working directory, in this case, ~/home/projects/example-project/ .


1 Answers

$ touch foo
$ ln -s foo bar
$ python
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> help(os.path.samefile)
Help on function samefile in module posixpath:

samefile(f1, f2)
    Test whether two pathnames reference the same actual file

>>> os.path.samefile("foo", "bar")
True
like image 56
Stefano Borini Avatar answered Sep 30 '22 07:09

Stefano Borini