zoukankan      html  css  js  c++  java
  • 使用OS模块来获取文件路径

    1.os模块概述

    Python os模块包含普遍的操作系统功能。如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的。

    2.常用方法

    os.getcwd()

    函数得到当前工作目录,即当前Python脚本工作的目录路径。

    os.listdir()

    返回指定目录下的所有文件和目录名。

    os.remove()

    删除一个文件。

    os.path.split()

    函数返回一个路径的目录名和文件名

    os.path.join(path,name,name1..):连接目录与文件名或目录,可以加多个目录

    以上方法比较常用,还有很多方法,有兴趣的可以查看官方文档。

    例子:

    import os
    class ReadFilePath:
        def read_file_path(self,first_dir,conf_dir):
            # 获取当前目录
            filepath = os.getcwd()
            #获取框架的顶层目录
            filepath1 = os.path.split(filepath)[0]
            # 然后进行目录的拼接
            path = os.path.join(filepath1,first_dir,conf_dir)
            return path

    参数说明:

    first_dir:框架下的的存放文件的目录名
    conf_dir:框架下要获取的文件名

    我这里只是按照自己的框架的目录,给大家提供一个参考。
  • 相关阅读:
    LeetCode 50: Pow(x, n)
    Codility 1: equilibrium
    LeetCode 49: Anagrams
    crackme160--(5)
    crackme160--(4)
    魔兽显血改键工具的脱壳和修改
    crackme160--(3)
    crackme160--(1)
    python-装饰器
    编程语言C++01-C++入门学习路线
  • 原文地址:https://www.cnblogs.com/666666pingzi/p/10369960.html
Copyright © 2011-2022 走看看