zoukankan      html  css  js  c++  java
  • python --os模块

    OS模块常用的函数:
    获取当前路径 os.getcwd()
    得到指定路径下的文件 和 目录名称 os.listdir('../')
    得到指定路径的绝对路径 os.path.abspath()
    对指定的路径分离(文件夹路径 + 文件名称 ) os.path.split()
    路径和文件合并  os.path.join()
    提取指定路径中的 文件夹 部分 和文件 名称 os.path.dirname()  os.path.basename()

    判断路径是存在os.path.exists()

    递归创建目录 os.makedirs()

    递归删除目录 os.removedirs()
    判断路径是否为目录  os.isdir()
    判断路径是否为文件 os.isfile()

     1 import numpy as np
     2 import os
     3 
     4 
     5 #路径操作
     6 def test01():
     7     #获取当前路径
     8     print("获取当前路径 os.getcwd():", os.getcwd() )
     9     #得到指定路径下的文件 和 目录名称
    10     print("得到指定路径下的文件 和 目录名称 os.listdir('../'):" , os.listdir('../'))
    11 
    12     #获取(指定路径的)绝对路径
    13     print( "得到指定路径的绝对路径 os.path.abspath:" ,os.path.abspath('../'))
    14 
    15     #对指定的路径分离(文件夹路径 + 文件名称 )
    16     print("#对指定的路径分离(文件夹路径 + 文件名称 )  os.path.split")
    17     os.path.split('/home/fh/图片/11-08/json_to_dataset.py')
    18 
    19     # 合并 os.path.join
    20     print("os.path.join, 路径和文件合并")
    21     os.path.join('/home/fh/图片/11-08/json_to_dataset.py', '/home/nufront/图片/11-08/getimg_label.py')
    22     os.path.join('/home/fh/图片/11-08', 'getimg_label.py')
    23     os.path.join('../dataset_learn', 'data_learn.py')
    24     os.path.join('..', 'dataset_learn')
    25 
    26     # 提取文件夹 部分 和文件 名称
    27     os.path.dirname('/home/fh/图片/11-08/json_to_dataset.py')
    28     os.path.basename('/home/fh/图片/11-08/json_to_dataset.py')
    29 
    30     # 查看目录或文件是否存在
    31     os.path.exists('/home/fh/图片/11-08/json_to_dataset.py')
    32     os.path.exists('/home/fh/图片/11-08')
    33     os.path.exists('/home/fh/图片/11-08/3423')
    34 
    35     # 创建一个目录
    36     os.mkdir('testdir')
    37     # 删除一个目录
    38     os.rmdir('testdir')
    39 
    40     #创建递归目录
    41     os.makedirs('./test/disrs/testdir')
    42     #递归删除目录
    43     os.removedirs('./test')
    44 
    45     print('改变当前目录   os.chdir()')
    46 
    47     print('判断路径是否为目录  os.isdir()')
    48 
    49     print('判断路径是否为文件 os.isfile()')
    50 
    51 
    52 if __name__ == 'main':
    53     test01()
    View Code
     
  • 相关阅读:
    [LeetCode] 1030. Matrix Cells in Distance Order 距离顺序排列矩阵单元格
    [LeetCode] 1029. Two City Scheduling 两个城市调度
    [LeetCode] 1027. Longest Arithmetic Subsequence 最长的等差数列
    [LeetCode] 1026. Maximum Difference Between Node and Ancestor 结点与其祖先之间的最大差值
    [LeetCode] 1025. Divisor Game 除数游戏
    一手遮天 Android
    一手遮天 Android
    一手遮天 Android
    一手遮天 Android
    一手遮天 Android
  • 原文地址:https://www.cnblogs.com/feihu-h/p/11841330.html
Copyright © 2011-2022 走看看