Python获取运行目录与当前脚本目录的方法

yipeiwu_com7年前Python基础

本文实例讲述了Python获取运行目录与当前脚本目录的方法。分享给大家供大家参考。具体实现方法如下:

import os
import sys
#运行目录
CurrentPath = os.getcwd()
print CurrentPath
#当前脚本目录
print "##################################################"
print os.path
print sys.argv[0]
print os.path.split( os.path.realpath( sys.argv[0] ) )
print "##################################################"
ScriptPath = os.path.split( os.path.realpath( sys.argv[0] ) )[0]
print ScriptPath

运行结果如下:

C:\pythondemo
##################################################
<module 'ntpath' from 'C:\Python27\lib\ntpath.pyc'>
C:/pythondemo/1.py
('C:\\pythondemo', '1.py')
##################################################
C:\pythondemo

希望本文所述对大家的python程序设计有所帮助。

相关文章

Python正则表达式使用范例分享

作为一个概念而言,正则表达式对于Python来说并不是独有的。但是,Python中的正则表达式在实际使用过程中还是有一些细小的差别。 本文是一系列关于Python正则表达式文章的其中一部...

Python numpy.array()生成相同元素数组的示例

如下所示: new_array = np.zeros((5,4)) for i in range(3): new_array[i] = np.array([0.25]*4) 运行...

Linux下用Python脚本监控目录变化代码分享

#!/usr/bin/env python #coding=utf-8 import os from pyinotify import WatchManager, Notifier...

Python中for循环和while循环的基本使用方法

while循环: while expression: suite_to_repeat while 条件:    语句块 不需要括号哦! >&g...

python通过apply使用元祖和列表调用函数实例

本文实例讲述了python通过apply使用元祖和列表调用函数的方法。分享给大家供大家参考。具体实现方法如下: def my_fuc(a, b): print a, b atup...