JiFu's Wiki JiFu's Wiki
首页
  • HTML
  • JavaScript
  • NodeJS
  • Vuejs
  • 微信小程序
  • Python
  • 数据库
  • 中间件
  • 算法
  • 软件工程
  • Wordpress
  • iOS开发
  • Android开发
  • Linux
  • Windows
  • MacOS
  • Docker
  • Vim
  • VSCode
  • Office
  • 其他
  • Photoshop
  • Sketch
  • Mac
  • 游戏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • HTML
  • JavaScript
  • NodeJS
  • Vuejs
  • 微信小程序
  • Python
  • 数据库
  • 中间件
  • 算法
  • 软件工程
  • Wordpress
  • iOS开发
  • Android开发
  • Linux
  • Windows
  • MacOS
  • Docker
  • Vim
  • VSCode
  • Office
  • 其他
  • Photoshop
  • Sketch
  • Mac
  • 游戏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 数据库

  • Python

    • Python介绍
    • 国内pip更换豆瓣的pypi源
    • sys.stdout.write实现Python控制台实时刷新打印
    • 发布你自己的轮子 - PyPI打包上传实践
    • Seaborn库绘制了17个超好看图表
    • Python脚本打包exe Auto-py-to-exe
    • Build-in

      • Python build-in functions
      • Python build-in functions - List
      • Python build-in functions - collections
      • Python build-in module - os
        • os.name
        • os.getcwd()
        • os.listdir()
        • os.remove()
        • os.system()
        • os.sep
        • os.linesep
        • os.path.split()
        • os.path.exists()
        • os.path.abspath(name)
        • os.path.normpath(path)
        • os.path.getsize(name)
        • os.path.splitext()
        • os.path.join(path,name)
        • os.path.basename(path)
        • path.dirname(path)
      • Python多线程和锁
    • Flask

    • Libaray

  • 中间件

  • 算法

  • 软件工程

  • Wordpress

  • 后端技术
  • Python
  • Build-in
JiFu
2023-10-18
目录

Python build-in module - os

# os模块 - 介绍

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

# os.name

输出字符串指示正在使用的平台。如果是window 则用'nt'表示,对于Linux/Unix用户,它是'posix'。

# os.getcwd()

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

# os.listdir()

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

>>> os.listdir(os.getcwd())
['Django', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'MySQL-python-wininst.log', 'NEWS.txt', 'PIL-wininst.log', 'python.exe', 'pythonw.exe', 'README.txt', 'RemoveMySQL-python.exe', 'RemovePIL.exe', 'Removesetuptools.exe', 'Scripts', 'setuptools-wininst.log', 'tcl', 'Tools', 'w9xpopen.exe']
1
2

# os.remove()

删除一个文件。

# os.system()

>>> os.system('dir')
0
>>> os.system('cmd') #启动dos
1
2
3

# os.sep

可以取代操作系统特定的路径分割符。

# os.linesep

字符串给出当前平台使用的行终止符

>>> os.linesep
'\r\n'            #Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'。
>>> os.sep
'\\'              #Windows
1
2
3
4

# os.path.split()

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

>>> os.path.split('C:\\Python25\\abc.txt')
('C:\\Python25', 'abc.txt')
9、os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。

>>> os.path.isdir(os.getcwd())
True
>>> os.path.isfile('a.txt')
False
1
2
3
4
5
6
7
8

# os.path.exists()

函数用来检验给出的路径是否真地存在

>>> os.path.exists('C:\\Python25\\abc.txt')
False
>>> os.path.exists('C:\\Python25')
True
1
2
3
4

# os.path.abspath(name)

获得绝对路径

# os.path.normpath(path)

规范path字符串形式

# os.path.getsize(name)

获得文件大小,如果name是目录返回0L

# os.path.splitext()

分离文件名与扩展名

>>> os.path.splitext('a.txt')
('a', '.txt')
1
2

# os.path.join(path,name)

连接目录与文件名或目录

>>> os.path.join('c:\\Python','a.txt')
'c:\\Python\\a.txt'
>>> os.path.join('c:\\Python','f1')
'c:\\Python\\f1'
1
2
3
4

# os.path.basename(path)

返回文件名

>>> os.path.basename('a.txt')
'a.txt'
>>> os.path.basename('c:\\Python\\a.txt')
'a.txt'
1
2
3
4

# path.dirname(path)

返回文件路径

>>> os.path.dirname('c:\\Python\\a.txt')
'c:\\Python'
1
2

# 参考资料

-python中os模块用法 (opens new window)

上次更新: 2024/08/11, 01:59:03
Python build-in functions - collections
Python多线程和锁

← Python build-in functions - collections Python多线程和锁→

最近更新
01
WPS快捷键
05-21
02
Disable notification "to get future google chrome updates you'll need macos 10.13 or later" on mac
05-14
03
MacOS软件推荐
04-30
更多文章>
Theme by Vdoing | Copyright © 2019-2025 Ji Fu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式