Convert Absolute to Relative File Path

  • Share this:

Code introduction


This function converts an absolute file path to a relative path. If the input path is already a relative path, it makes no changes; if the input is an absolute path, it converts it to a relative path relative to the current working directory.


Technology Stack : os

Code Type : Function

Code Difficulty : Intermediate


                
                    
import re
import math
import sys
import os

def process_file_path(file_path):
    """
    将文件路径转换为相对路径。
    """
    if not os.path.isabs(file_path):
        return file_path
    else:
        return os.path.relpath(file_path, start=os.getcwd())                
              
Tags: