码迷,mamicode.com
首页 > 其他好文 > 详细

5lession-path路径相关操作

时间:2015-05-28 15:45:07      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

今天开始接触到了文件目录、路径方面的知识点。记录如下

先看代码

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
current_path = os.getcwd()
path_test = "/home/zhangshuli/PROJECT/PROJECTL/32_kk"
path_test2 = "/home/zhangshuli/PROJECT/PROJECTL/32_kk/packages/apps/Calculator"

print(current_path)

os.chdir(path_test)
print(current_path)

current_path = os.getcwd()
print(current_path)

current_path = os.listdir(path_test)
print(current_path)

current_path = os.path.split(path_test)
print(current_path)

os.chdir(path_test2)
current_path = os.path.abspath(os.curdir)
print(current_path)

os.chdir(path_test2)
current_path = os.path.abspath(.)
print(current_path)


current_path = os.path.abspath(..)
print(current_path)

current_path = sys.argv[0]
print(current_path)

 

运行结果如下

/home/zhangshuli/desktop/python_test
/home/zhangshuli/desktop/python_test
/home/zhangshuli/PROJECT/PROJECTL/32_kk
[ndk, makeMtk.ini, mediatek, bionic, dalvik, libcore, vanzo_custom_base, bootable, hardware, mbldenv.sh, system, mk, external, makeMtk, Makefile, checkenv.log, packages, apks, out, development, recommend_apks, pdk, r1.txt, auto_sync_android.log, art, sdk, abi, docs, libnativehelper, log, kernel, update_overlay_files.py, .repo, frameworks, device, vanzo_common2.pyc, build, vendor, vanzo_common2.py, prebuilts]
(/home/zhangshuli/PROJECT/PROJECTL, 32_kk)
/home/zhangshuli/PROJECT/PROJECTL/32_kk/packages/apps/Calculator
/home/zhangshuli/PROJECT/PROJECTL/32_kk/packages/apps/Calculator
/home/zhangshuli/PROJECT/PROJECTL/32_kk/packages/apps
/home/zhangshuli/desktop/python_test/path.py

相关知识点总结:

1.os.chdir("path"):切换到path目录

2.os.getcwd():获得当前工作目录

如果你使用os.chdir()切换了目录,os.getcwd()会跟着改变

3.os.path.split(path_test):把path_test进行拆分,得到的是当前目录名跟它的父类路径

4.os.path.abspath(os.curdir):获得当前工作路径

也可以使用这个方法

os.path.abspath(‘.‘)

. 代表当前路径

5.os.path.abspath(‘..‘):获得当前目录的父目录路径

..代表上一级目录

6.sys.argv[0]:获得运行脚本你本身所在的目录。

 

5lession-path路径相关操作

标签:

原文地址:http://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_path_15528144.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!