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

yipeiwu_com5年前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编程使用*解包和itertools.product()求笛卡尔积的方法

本文实例讲述了Python编程使用*解包和itertools.product()求笛卡尔积的方法。分享给大家供大家参考,具体如下: 【问题】 目前有一字符串s = "['a', 'b']...

Python splitlines使用技巧

复制代码 代码如下:mulLine = """Hello!!! Wellcome to Python's world! There are a lot of interesting th...

Python 文件管理实例详解

本文实例讲述了Python 文件管理的方法。分享给大家供大家参考,具体如下: 一、Python中的文件管理 文件管理是很多应用程序的基本功能和重要组成部分。Python可以使文件管理极其...

python用for循环求和的方法总结

Python中可以使用for循环实现累加求和 for循环语法: for 变量 in range(x): 循环需要执行的代码 如下实现1到n求和: def main():...

Python Pillow Image Invert

本文主要是利用Python的第三方库Pillow,实现单通道灰度图像的颜色翻转功能。 # -*- encoding:utf-8 -*- import os import sys fr...