将绝对路径转相对路径,其实os.path.relpath(““)直接搞定。不过自己试着写了一个

import os

def absdir_to_reldir(absdir):
    # absdir
    abs_curdir = os.path.abspath(os.curdir)

    a_list = absdir.split("\\")
    b_list = abs_curdir.split("\\")

    print(a_list)
    print(b_list)
    len_a_list = len(a_list)  # ['D:', 'a1', 'b1', 'c1', 'd1', 'a.txt']
    len_b_list = len(b_list)  # ['D:', 'a1', 'b1', 'c1', 'd1']

    new_a_list = a_list.copy()
    new_b_list = b_list.copy()

    rel_index = 0
    for i in range(min(len_a_list, len_b_list)):
        if a_list[i] != b_list[i]:
            rel_index = i
            break
        else:
            rel_index = i + 1
    new_a_list = new_a_list[rel_index:]
    new_b_list = new_b_list[rel_index:]
    print(f"new_a_list: {new_a_list}")
    print(f"new_b_list: {new_b_list}")

    if len(new_b_list) == 0:
        up_dir = "./"
    else:
        up_dir = "../" * len(new_b_list)

    down_dir = "/".join(new_a_list)
    print(f"up_dir: {up_dir}")
    print(f"down_dir: {down_dir}")
    reldir = up_dir + down_dir
    # print(f"reldir: {reldir}")

    return reldir

reldir = absdir_to_reldir(r"D:\_1study\code\Python\web\cxwproject_01\serviceApp\urls.py")
print(f"reldir: {reldir}")  # ..\..\..\Python\web\cxwproject_01\serviceApp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<VisualStudioProject> <CSHARP ProjectType = "Local" ProductVersion = "7.10.3077" SchemaVersion = "2.0" ProjectGuid = "{A5EE1DF1-9D33-4541-AE99-BC09713525FB}" > <Build> <Settings ApplicationIcon = "..\Icon\hrm.ico" AssemblyKeyContainerName = "" AssemblyName = "人力资源管理系统" AssemblyOriginatorKeyFile = "" DefaultClientScript = "JScript" DefaultHTMLPageLayout = "Grid" DefaultTargetSchema = "IE50" DelaySign = "false" OutputType = "WinExe" PreBuildEvent = "" PostBuildEvent = "" RootNamespace = "人力资源管理系统" RunPostBuildEvent = "OnBuildSuccess" StartupObject = "" > <Config Name = "Debug" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "DEBUG;TRACE" DocumentationFile = "" DebugSymbols = "true" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "false" OutputPath = "bin\Debug\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> <Config Name = "Release" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "TRACE" DocumentationFile = "" DebugSymbols = "false" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "true" OutputPath = "bin\Release\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> </Settings> <References> <Reference Name = "System" AssemblyName = "System" HintPath = "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.dll" /> <Reference Name = "System.Data" AssemblyName = "System.Data" HintPath = "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.Data.dll" /> <Reference Name = "System.Drawing" AssemblyName = "System.Drawing" HintPath = "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.Drawing.dll" /> <Reference Name = "System.Windows.Forms" AssemblyName = "System.Windows.Forms" HintPath = "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.Windows.Forms.dll" /> <Reference Name = "System.XML" AssemblyName = "System.Xml" HintPath = "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.XML.dll" /> </References> </Build> <Files> <Include> <File RelPath = "AmendStafferInfo.cs" SubType = "Form" BuildAction = "Compile" /> <File RelPath = "AmendStafferInfo.resx" DependentUpon = "AmendStafferInfo.cs" BuildAction = "EmbeddedResource" /> <File RelPath = "AssemblyInfo.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "DataGridNoActiveCellColumn.cs" SubType = "Component" BuildAction = "Compile" /> <File RelPath = "DataGridNoActiveCellColumn.resx" DependentUpon = "DataGridNoActiveCellColumn.cs" BuildAction = "EmbeddedResource" /> <File RelPath = "DataSet1.xsd" BuildAction = "Content" Generator = "MSDataSetGenerator" LastGenOutput = "DataSet1.cs" /> <File RelPath = "DataSet1.cs" DependentUpon = "DataSet1.xsd" SubType = "code" BuildAction = "Compile" DesignTime = "True" AutoGen = "True" /> <File RelPath = "DataSet1.xsx" DependentUpon = "DataSet1.xsd" BuildAction = "None" /> <File RelPath = "DataSet11.xsd" BuildAction = "Content" Generator = "MSDataSetGenerator" LastGenOutput = "DataSet11.cs" /> <File RelPath = "DataSet11.cs" DependentUpon = "DataSet11.xsd" SubType = "code" BuildAction = "Compile" DesignTime = "True" AutoGen = "True" /> <File RelPath = "DataSet2.xsd" BuildAction = "Content" Generator = "MSDataSetGenerator" LastGenOutput = "DataSet2.cs" /> <File RelPath = "DataSet2.cs" DependentUpon = "DataSet2.xsd" SubType = "code" BuildAction = "Compile" DesignTime = "True" AutoGen = "True" /> <File RelPath = "DataSet2.xsx" DependentUpon = "DataSet2.xsd" BuildAction = "None" /> <File RelPath = "HortationManage.cs" SubType = "Form" BuildAction = "Compile" /> <File RelPath = "HortationManage.resx" DependentUpon = "HortationManage.cs" BuildAction = "EmbeddedResource" /> <File RelPath = "hrm.ico" BuildAction = "Content" /> <File RelPath = "IncomeTax.cs" SubType = "Form" BuildAction = "Compile" /> <File RelPath = "IncomeTax.resx" DependentUpon = "IncomeTax.cs" BuildAction = "EmbeddedResource" /> <File RelPath = "LookupStafferInfo.cs" SubType = "Form" BuildAction = "Compile" /> <File RelPath = "LookupStafferInfo.resx" DependentUpon = "LookupStafferInfo.cs" BuildAction = "EmbeddedResource" /> <File RelPath = "MainFrm.cs" SubType = "Form" BuildAction = "Compile" /> <File RelPath = "MainFrm.resx" DependentUpon = "MainFrm.cs" BuildAction = "EmbeddedResource" /> <File RelPath = "OrgInfo.cs" SubType = "Form" BuildAction = "Compile" /> <File RelPath = "OrgInfo.resx" DependentUpon = "OrgInfo.cs" BuildAction = "EmbeddedResource" /> <File RelPath = "PayoffHistory.cs" SubType = "Form" BuildAction = "Compile" /> <File RelPath = "PayoffHistory.resx" DependentUpon = "PayoffHistory.cs" BuildAction = "EmbeddedResource" /> <File RelPath = "PunishmentManage.cs" SubType = "Form" BuildAction = "Compile" /> <File RelPath = "PunishmentManage.resx" DependentUpon = "PunishmentManage.cs" BuildAction = "EmbeddedResource" /> <File RelPath = "SalaryManage.cs" SubType = "Form" BuildAction = "Compile" /> <File RelPath = "SalaryManage.resx" DependentUpon = "SalaryManage.cs" BuildAction = "EmbeddedResource" /> <File RelPath = "SetOrgInfo.cs" SubType = "Form" BuildAction = "Compile" /> <File RelPath = "SetOrgInfo.resx" DependentUpon = "SetOrgInfo.cs" BuildAction = "EmbeddedResource" /> </Include> </Files> </CSHARP> </VisualStudioProject>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值