python ---> relative and absolute paths

Absolute path (absolute path): starts from the roots

  eg:c:\file\01.txt

 

Relative path (relative path): Relative to find in the current document

  On a current file ../ #

 

os.path.isabs (path): determine whether the path is an absolute path

  Returns True, that is the absolute path

  Returns False, that is, the relative path

 

eg: file hierarchy is as follows:

  | --- file01

    |---day01.txt

    |---day02.txt

  | --- file02

    |---day01.txt

    |---day02.txt

  Suppose the current directory is file01 / day01.txt

  Demand 1: Find day02.txt under file01

  r = os.path.isabs(file01.txt)

  Requirement 2: Find day02.txt under file02

  r = os.path.isabs (../ 01.txt) # return a look inside

 

Note: Make sure a good level of relations

 

Get the path:

1. The relative path, an absolute path to give

  path = os.path.abspath('day01.txt')  

2. Get the absolute path of the current file

  path = os.path.abspath(__file__)

3. To get the directory of the current file

  path = os.getcwd () # is equivalent to: path = os.path.dirname (__ file__)

Guess you like

Origin www.cnblogs.com/abner-pan/p/11876828.html