函数名 | 功能 |
---|---|
os.name | 指示用户正在使用的工作平台 |
os.getcwd ( ) | 获取当前的工作目录 |
os.listdir ( ) | 返回指定目录下的所有文件和目录名 |
os.remove (file) | 删除指定文件 |
os.rename (‘a.txt’,‘b.txt’) | 对文件进行重命名 |
os.mkdir (name) | 创建目录 |
os.makedirs ( ) | 递归创建目录 |
os.rmdir (name) | 删除目录 |
os.removedirs (name) | 递归删除多个目录 |
os.system ( ) | 通过python来调用控制台命令 |
os.path.split ( ) | 返回一个路径的目录名和文件名 |
os.path.join (path,name) | 连接目录与文件名 |
os.path.dirname (path) | 返回文件路径 |
os.path.exists (name) | 判断是否存在文件或目录 |
os.path.isdir (path) | 判断参数是否为目录 |
os.stat (file) | 获取文件属性(有多个属性) |
os.stat (file).st_size | 获取文件大小 |
>>> import os
>>> os.name
‘nt‘
>>> os.getcwd()
‘C:\\Users\\lenovo\\AppData\\Local\\Programs\\Python\\Python38‘
>>> os.listdir("c:\\")
[‘$Recycle.Bin‘, ‘Documents and Settings‘, ‘hiberfil.sys‘, ‘inetpub‘, ‘Intel‘, ‘KDubaSoftDownloads‘, ‘pagefile.sys‘, ‘PerfLogs‘, ‘Program Files‘, ‘Program Files (x86)‘, ‘ProgramData‘, ‘Recovery‘, ‘swapfile.sys‘, ‘System Volume Information‘, ‘tmp‘, ‘Users‘, ‘Windows‘]
>>> os.remove("e:\\test.txt")
结果:
删除前
删除后
>>> os.rename("e:\\a.txt","e:\\b.txt")
结果:
重命名前
重命名后
>>> os.mkdir("e:\\test")
结果:
>>> os.makedirs("e:\\a\\b")
结果:
>>> os.rmdir("e:\\test")
结果:
删除前
删除后
>>> os.removedirs("e:\\a\\b")
结果:
删除前
删除后:
>>> os.system(‘dir‘)
驱动器 C 中的卷没有标签。
卷的序列号是 90F6-555F
C:\Users\lenovo\AppData\Local\Programs\Python\Python38 的目录
2020/02/16 19:32 <DIR> .
2020/02/16 19:32 <DIR> ..
2020/02/16 19:32 <DIR> DLLs
2020/02/16 19:32 <DIR> Doc
2020/02/16 19:32 <DIR> include
2020/02/16 19:32 <DIR> Lib
2020/02/16 19:32 <DIR> libs
2019/12/18 23:27 31,322 LICENSE.txt
2019/12/18 23:27 879,215 NEWS.txt
2019/12/18 23:27 99,912 python.exe
2019/12/18 23:27 58,952 python3.dll
2019/12/18 23:27 4,191,304 python38.dll
2019/12/18 23:27 98,376 pythonw.exe
2020/05/16 10:50 <DIR> Scripts
2020/02/16 19:32 <DIR> tcl
2020/02/16 19:32 <DIR> Tools
2019/12/18 23:27 89,752 vcruntime140.dll
7 个文件 5,448,833 字节
10 个目录 34,787,536,896 可用字节
0
12345678910111213141516171819202122232425
>>> os.path.split("e:\\a\\b")
(‘e:\\a‘, ‘b‘)
>>> os.path.join("e:\\test","a.txt")
‘e:\\test\\a.txt‘
>>> os.path.dirname("e:\\a\\b")
‘e:\\a‘
>>> os.path.exists("e:\\a\\b")
True
>>> os.path.exists("e:\\a\\bb1")
False
>>> os.path.isdir("e:\\")
True
>>> os.path.isdir("b")
False
>>> os.stat("e:\\a\\b\\test.txt")
os.stat_result(st_mode=33206, st_ino=2814749767256628, st_dev=586237654, st_nlink=1, st_uid=0, st_gid=0, st_size=28, st_atime=1593337361, st_mtime=1593337372, st_ctime=1593337361)
>>> os.stat("e:\\a\\b\\test.txt").st_size
28
原文:https://www.cnblogs.com/geoffreygao/p/13637830.html