1.绝对路径转相对路径
print os.path.relpath("d:/MyProj/MyFile.txt") #..\MyProj\MyFile.txt
是根据当前路径的相对路径
2.相对路径转绝对路径
注意用os.chdir(dir)改变当前比较路径
path = "..\MyProj\MyFile.txt" print os.path.abspath(path) #D:\MyProj\MyFile.txt
**
**
看以下实例
# abspath() 将路径转换为绝对路径
# relpath() 将路径转化为相对路径
# . 点号代表当前目录
# .. 双点号代表父目录
import os
absp = os.path.abspath('..')
print(absp)
absp = os.path.abspath('.')
print(absp)
resp = os.path.relpath('.')
print(resp)
输出结果:
D:\Hello World\python_work\TLXY_study_note
D:\Hello World\python_work\TLXY_study_note\Package_Module
.
print os.path.relpath("d:/MyProj/MyFile.txt") #..\MyProj\MyFile.txt
是根据当前路径的相对路径
注意用os.chdir(dir)改变当前比较路径
path = "..\MyProj\MyFile.txt" print os.path.abspath(path) #D:\MyProj\MyFile.txt
转载于:https://www.cnblogs.com/hont/p/5412432.html
展开全部
服务器中的Java类获得当前路径Weblogic WebApplication的系统文件根目录32313133353236313431303231363533e78988e69d8331333337616634是你的weblogic安装所在根目录。
例如:如果你的weblogic安装在c:\bea\weblogic700…… 那么,你的文件根路径就是c:\. 所以,有两种方式能够让你访问你的服务器端的文件:
a.使用绝对路径: 比如将你的参数文件放在c:\yourconfig\yourconf.properties, 直接使用 new FileInputStream("yourconfig/yourconf.properties");
b.使用相对路径: 相对路径的根目录就是你的webapplication的根路径,即WEB-INF的上一级目录,将你的参数文件放
在yourwebapp\yourconfig\yourconf.properties, 这样使用: new
FileInputStream("./yourconfig/yourconf.properties"); 这两种方式均可,自己选择。
(2)。Tomcat 在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin
(3)。Resin 不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET 的路径为根。比如用新建文件法测试File f = new File("a.htm"); 这个a.htm在resin的安装目录下
(4)。如何读相对路径哪? 在Java文件中getResource或getResourceAsStream均可
例:getClass()。getResourceAsStream(filePath);//filePath可以是"/filename",这
里的/代表web 发布根路径下WEB-INF/classes 默认使用该方法的路径是:WEB-INF/classes.已经在Tomcat中测试。
在一份代码中看到了一个路径转换的方法,贴出来共享。具体是谁的代码也不知道,如有冒犯请留言。
/// <summary>
/// 绝对路径转相对路径
/// </summary>
/// <param name="strBasePath">基本路径</param>
/// <param name="strFullPath">绝对路径</param>
/// <returns>strFullPath相对于strBasePath的相对路径</returns>
public static string GetRelativePath(string strBasePath, string strFullPath)
{
if (strBasePath == null)
throw new ArgumentNullException("strBasePath");
if (strFullPath == null)
throw new ArgumentNullException("strFullPath");
strBasePath = Path.GetFullPath(strBasePath);
strFullPath = Path.GetFullPath(strFullPath);
var DirectoryPos = new int[strBasePath.Length];
int nPosCount = 0;
DirectoryPos[nPosCount] = -1;
++nPosCount;
int nDirectoryPos = 0;
while (true)
{
nDirectoryPos = strBasePath.IndexOf('\\', nDirectoryPos);
if (nDirectoryPos == -1)
break;
DirectoryPos[nPosCount] = nDirectoryPos;
++nPosCount;
++nDirectoryPos;
}
if (!strBasePath.EndsWith("\\"))
{
DirectoryPos[nPosCount] = strBasePath.Length;
++nPosCount;
}
int nCommon = -1;
for (int i = 1; i < nPosCount; ++i)
{
int nStart = DirectoryPos[i - 1] + 1;
int nLength = DirectoryPos[i] - nStart;
if (string.Compare(strBasePath, nStart, strFullPath, nStart, nLength, true) != 0)
break;
nCommon = i;
}
if (nCommon == -1)
return strFullPath;
var strBuilder = new StringBuilder();
for (int i = nCommon + 1; i < nPosCount; ++i)
strBuilder.Append("..\\");
int nSubStartPos = DirectoryPos[nCommon] + 1;
if (nSubStartPos < strFullPath.Length)
strBuilder.Append(strFullPath.Substring(nSubStartPos));
string strResult = strBuilder.ToString();
return strResult == string.Empty ? ".\\" : strResult;
}
用法:
using System;
using System.Text;
using System.IO;
namespace PathDemo
{
class MainClass
{
public static void Main (string[] args)
{
string str = GetRelativePath ("/Users/apple/Documents/TempDemo/PathDemo", "/Users/apple/Documents/TempDemo/PathDemo/PathDemo/Main.cs");
Console.WriteLine ("********");
Console.WriteLine (str);
Console.WriteLine ("********");
}
public static string GetRelativePath(string strBasePath, string strFullPath)
{
if (strBasePath == null)
throw new ArgumentNullException("strBasePath");
if (strFullPath == null)
throw new ArgumentNullException("strFullPath");
strBasePath = Path.GetFullPath(strBasePath);
strFullPath = Path.GetFullPath(strFullPath);
var DirectoryPos = new int[strBasePath.Length];
int nPosCount = 0;
DirectoryPos[nPosCount] = -1;
++nPosCount;
int nDirectoryPos = 0;
while (true)
{
nDirectoryPos = strBasePath.IndexOf('\\', nDirectoryPos);
if (nDirectoryPos == -1)
break;
DirectoryPos[nPosCount] = nDirectoryPos;
++nPosCount;
++nDirectoryPos;
}
if (!strBasePath.EndsWith("\\"))
{
DirectoryPos[nPosCount] = strBasePath.Length;
++nPosCount;
}
int nCommon = -1;
for (int i = 1; i < nPosCount; ++i)
{
int nStart = DirectoryPos[i - 1] + 1;
int nLength = DirectoryPos[i] - nStart;
if (string.Compare(strBasePath, nStart, strFullPath, nStart, nLength, true) != 0)
break;
nCommon = i;
}
if (nCommon == -1)
return strFullPath;
var strBuilder = new StringBuilder();
for (int i = nCommon + 1; i < nPosCount; ++i)
strBuilder.Append("..\\");
int nSubStartPos = DirectoryPos[nCommon] + 1;
if (nSubStartPos < strFullPath.Length)
strBuilder.Append(strFullPath.Substring(nSubStartPos));
string strResult = strBuilder.ToString();
return strResult == string.Empty ? ".\\" : strResult;
}
}
}