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 xml.etree.ElementTree遍历xml所有节点实例详解

python xml.etree.ElementTree遍历xml所有节点 XML文件内容: <students> <student name='刘备' s...

python中json格式数据输出的简单实现方法

主要使用json模块,直接导入import json即可。 小例子如下: #coding=UTF-8 import json info={} info["code"]=...

python 批量添加的button 使用同一点击事件的方法

python 批量添加的button 使用同一点击事件根据传递的参数进行区分。 def clear_text(): print '我只是个清空而已' def clear_tex...

解决python 输出是省略号的问题

这个问题非常非常重要,搞了一晚上都没解决好,但是真的很简单很简单, 如果你也 是用的numpy array, 如果你也想得到输出矩阵的全部内容,而不是省略形式, [[ 0.10284...

python 使用装饰器并记录log的示例代码

1.首先定义一个log文件 # -*- coding: utf-8 -*- import os import time import logging import sys log_d...