• 5lession-path路径相关操作


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

    先看代码

    #!/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]:获得运行脚本你本身所在的目录。

  • 相关阅读:
    快速幂模板
    洛谷题解 P1596 [USACO10OCT]Lake Counting S
    洛谷题解 P1460 [USACO2.1]健康的荷斯坦奶牛 Healthy Holsteins
    洛谷题解 P1305 新二叉树
    洛谷题解 P1075 质因数分解
    洛谷题解 P1331 海战
    洛谷题解 P1506 拯救oibh总部
    【洛谷P4831】Scarlet loves WenHuaKe
    【洛谷P6499】Burza
    【CF757F】Team Rocket Rises Again
  • 原文地址:https://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_path_15528144.html
Copyright © 2020-2023  润新知