Python编程:os模块

简介: Python编程:os模块

os属性

import os
print(os.curdir) # 当前目录 .
print(os.pardir) # 父目录 ..
print(os.name)  # 平台名称 win nt linux posix
print(os.sep)  # separator路径分隔符win \\ linux /
print(os.linesep) # 行终止符 win \r\n  linux \n
print(os.pathsep)  # 分隔文件路径 ;
print(os.extsep)  # extension扩展名 .

os方法

import os
print(os.getcwd())  # 返回当前工作目录
print(os.chdir( r"dir" ))  # 改变工作目录,相当于shell下cd
os.makedirs("dir1/dir2")  # 生成多层递归目录,存在则无法创建
os.removedirs("dir1/dir2")  # 递归删除空目录
os.mkdir("dirname")  # 生成单级目录,存在则无法创建
os.rmdir("dirname")  # 删除单级空目录,不为空则无法删除
print(os.listdir())  # list返回指定目录下所有文件和子目录,包括隐藏文件
os.remove("file.txt") # 删除一个文件
os.rename("oldname", "newname")  # 重命名文件/目录
os.system("ipconfig /all")  # 运行shell命令,直接显示
print(os.environ) # 返回系统环境变量

path模块

import os
print(os.path.abspath("file.txt"))  #返回绝对路径
print(os.path.split("dirname/file.txt"))  #tuple分隔目录和文件名
print(os.path.splitext("file.txt"))  #tuple分隔文件名和扩展名  (file, .txt)
print(os.path.dirname("dirname/file.txt"))  #返回目录
print(os.path.basename("dirname/file.txt")) # 返回文件名
print(os.path.join("dir","file.txt")) # 返回一个组合路径
print(os.path.exists("file.txt")) # bool判断文件是否存在
print(os.path.isabs("file.txt")) # bool判断是否为绝对路径
print(os.path.isfile("file.txt")) # bool判断是否为一个文件
print(os.path.isdir("dir")) # bool判断是否为一个目录
print(os.stat("file.txt"))  # tuple获取文件/目录信息
print(os.path.getatime("file.txt")) # 时间戳,返回最后存取时间
print(os.path.getmtime("file.txt")) # 时间戳,返回最后修改时间
# 时间戳转为格式化时间字符串
# time.strftime("%Y-%m-%d %H:%M:%S",time.localtime("timestamp")

遍历所有文件和文件夹

import os
# 游走目录树
for root, dirs, files in os.walk("."):
    for file in files:
        print(os.path.join(root, file))
    for dir in dirs:
        print(os.path.join(root, dir))

help(os)

  • all functions from posix, nt or ce, e.g. unlink, stat, etc.
  • os.path is either posixpath or ntpath
  • os.name is either ‘posix’, ‘nt’ or ‘ce’.
  • os.curdir is a string representing the current directory (‘.’ or ‘:’)
  • os.pardir is a string representing the parent directory (‘..’ or ‘::’)
  • os.sep is the (or a most common) pathname separator (‘/’ or ‘:’ or ‘\’)
  • os.extsep is the extension separator (always ‘.’)
  • os.altsep is the alternate pathname separator (None or ‘/’)
  • os.pathsep is the component separator used in $PATH etc
  • os.linesep is the line separator in text files (‘\r’ or ‘\n’ or ‘\r\n’)
  • os.defpath is the default search path for executables
  • os.devnull is the file path of the null device (‘/dev/null’, etc.)


相关文章
|
5月前
|
Python
Python教程:os 与 sys 模块详细用法
os 模块用于与操作系统交互,主要涉及夹操作、路径操作和其他操作。例如,`os.rename()` 重命名文件,`os.mkdir()` 创建文件夹,`os.path.abspath()` 获取文件绝对路径等。sys 模块则用于与 Python 解释器交互,常用功能如 `sys.path` 查看模块搜索路径,`sys.platform` 检测操作系统等。这些模块提供了丰富的工具,便于开发中处理系统和文件相关任务。
211 14
|
11月前
|
Python
Python实用记录(四):os模块-去后缀或者改后缀/指定目录下图片或者子目录图片写入txt/csv
本文介绍了如何使用Python的os模块来操作文件,包括更改文件后缀、分割文件路径和后缀、将指定目录下的所有图片写入txt文档,以及将指定目录下所有子目录中的图片写入csv文档,并为每个子目录分配一个标签。
155 1
|
11月前
|
Shell Python
Python 的 os 库的应用实例
Python 的 os 库的应用实例
147 3
|
10月前
|
JavaScript 前端开发 Python
python中的OS模块的基本使用
欢迎来到瑞雨溪的博客,一名热爱JavaScript与Vue的大一学生。博客分享前端技术及全栈开发经验,持续更新中,期待您的关注和支持!🎉🎉🎉
130 0
|
11月前
|
安全 测试技术 数据库
Python编程--sys模块及OS模块简单用例
Python编程--sys模块及OS模块简单用例
115 1
|
11月前
|
JSON 数据格式 Python
Python编程:利用JSON模块编程验证用户
Python编程:利用JSON模块编程验证用户
87 1
|
11月前
|
数据处理 Python
Python编程-利用datetime模块生成当前年份之前指定的间隔所有年份的日期列表和csv文件
Python编程-利用datetime模块生成当前年份之前指定的间隔所有年份的日期列表和csv文件
121 1
|
12月前
|
Python
python之os模块
python之os模块
|
11月前
|
Shell Python
Python中os模块的常用方法和示例
在Python中,`os`模块提供了与操作系统交互的函数,用于文件和目录管理、路径操作、环境变量等。常用方法包括路径操作(如`os.path.join()`、`os.path.abspath()`)、文件和目录管理(如`os.mkdir()`、`os.remove()`)、环境变量和进程管理(如`os.getenv()`、`os.system()`)以及其他常用功能(如`os.getcwd()`、`os.urandom()`)。
295 0
|
11月前
|
存储 JSON 数据格式
Python 输入输出与文件处理: io、pickle、json、csv、os.path 模块详解
Python 输入输出与文件处理: io、pickle、json、csv、os.path 模块详解
132 0

推荐镜像

更多