刘总的笔记小站

生活常识,娱乐搞笑,编程技巧,智能家居,深度学习,网络神经,数据挖掘

各种语言获取运行路径和程序路径

所谓的当前,有两种理解,一种是启动运行路径,一种脚本所在路径,此处指获取执行目标所在路径。


0,C语言获取当前工作路径和程序所在路径

#include  <unistd.h>
main(int argc, char * argv[])
{
    char buf[80];   
    getcwd(buf,sizeof(buf));   // current working directory

    char  resolved_path[80];
    realpath(argv[0],resolved_path);// NG run at /home put in /us/bin PATH env
    printf("resolved_path: %s\n", resolved_path);
    sleep(300);
    
    char buf[ MAXBUFSIZE ]; 
    int count = readlink( "/proc/self/exe", buf, MAXBUFSIZE ); 

}


1,win批处理bat脚本获取脚本所在的路径(注意:区别运行路径,涉及脚本读取当前相对路径文件)

set curDisk=%~d0
set curDirs=%~dp0
set runDirs=%cd%
set tarDirs=tar

::set PATH=C:\Python27\;$PATH

echo "curDisk=%curDisk%" 
echo "curDirs=%curDirs%" 
echo "runDirs=%runDirs%" 
echo "tarDirs=%tarDirs%"


%cd%      C:\and
%0         autorun.bat
%~d0       C:
%~p0       \and\
%~dp0      C:\and\
%~n0       autorun
%~x0       .bat
%~f0       C:\and\autorun.bat
%~s0       C:\and\autorun.bat
%~fs0      C:\and\autorun.bat
%~a0       --a--------
%~t0       2018/08/13 16:19
%~z0       897
%~ftza0    --a-------- 2018/08/13 16:19 897 C:\and\autorun.bat
%~$PATH:0

echo %UserProfile%          C:\Users\Administrator
echo %AllUsersProfile%      C:\ProgramData
echo %SystemRoot%           C:\Windows
echo %temp%                 C:\Users\ADMINI~1\AppData\Local\Temp
echo %ProgramFiles%         C:\Program Files

2,linux shell脚本获取脚本所在的路径(注意:区别运行路径,涉及脚本读取当前相对路径文件)

常用获取路径和文件命令:
dirname ./git/as.sh  # ./git
basename ./git/as.sh # as.sh

路径拼接获取:
argvvar=$0
basevar=$(pwd)/${argvvar#*/}
basevar=$(dirname $basevar)

路径切换获取:
basedir=`cd \`dirname $0\`; pwd`
basepath=$(cd `dirname $0`; pwd)


3,python脚本获取脚本所在的路径(注意:区别运行路径,涉及脚本读取当前相对路径文件)

    import os
    allPath = os.path.split(os.path.realpath(__file__))
    scrPath = os.path.dirname(os.path.abspath(__file__))
    runPath = os.getcwd()
    #curName = os.path.basename(__file__)
    #curPath = os.path.dirname(__file__)
    curName = allPath[1]
    curPath = allPath[0]
    farPath = os.path.dirname(curPath)

    print('curName=' + curName)
    print('curPath=' + curPath)
    print('farPath=' + farPath)
    print('runPath=' + runPath)


4,node js脚本获取脚本所在的路径,注意区别运行路径

__filename
__dirname

// D:\  $ node D:\node\zx.js
// D:\node  $ node D:\node\zx.js
console.log(__filename); // D:\node\zx.js
console.log(__dirname); // D:\node


5,java获取运行路径和java获取当前class文件所在路径

public class PathHelloWorld{
	public static void main (String[] args)	{
		//PathHelloWorld pathHelloWorld = new PathHelloWorld();
		
		String relativelyPath=System.getProperty("user.dir");
		System.out.println( relativelyPath );
		
		String classFilePath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); 
		System.out.println( classFilePath );

        try {
    		String jarWholePath = PathHelloWorld.class.getProtectionDomain().getCodeSource().getLocation().getFile();
    		jarWholePath = java.net.URLDecoder.decode(jarWholePath, "UTF-8");
    		String jarFilePath = new java.io.File(jarWholePath).getParentFile().getAbsolutePath();
    		System.out.println( jarFilePath );
	    } catch (java.io.IOException e) { 
	            e.printStackTrace(); 
	    }
	}
}
// javaw PathHelloWorld.java && java PathHelloWorld


发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
搜索
«   2024年9月   »
1
2345678
9101112131415
16171819202122
23242526272829
30
网站分类
最新留言
文章归档
网站收藏
友情链接
图标汇集
Powered by Z-BlogPHP

  • Copyright ©2021 @liuzong All rights reserved.
  • 陕ICP备17016542号