Python os模块常用函数

本文介绍了Python os模块的常用函数,包括获取当前工作目录、返回指定文件夹内容列表、处理路径、运行shell命令、创建和删除目录及文件、改变工作目录、查看文件时间和状态等,为Python开发者提供了实用的路径和文件操作方法。

Python os模块常用函数

1. os.getcwd():获取当前的工作目录
getcwd()方法语法格式如下:os.getcwd()  
import os, sys

# 打印当前目录
print("当前工作目录 : %s" % os.getcwd())
当前工作目录 : F:\Proiect\Python-Learning\python\path
2. os.listdir(): 用于返回指定文件夹包含的文件或文件夹的名字的列表
import os, sys

# 打开文件
path = 'F:/Proiect/Python-Learning/python'
dirs = os.listdir(path)
# 输出所有文件和文件夹
for file in dirs:
    print(file)    
.idea
Data
file
hello.txt
Matplotlib
matplotlib.xmind
numpy
numpy.xmind
Pandas
path
Python_base
类与对象
3. os.path.abspath(path) :返回绝对路径
import os, sys

path1 = os.path.abspath(r".")
print(path1)
path2 = os.path.abspath(r"..")
print(path2)
F:\Proiect\Python-Learning\python\path
F:\Proiect\Python-Learning\python
4.os.system() :运行shell命令
import os, sys

os.system('cmd')
os.system('ls')
5. os.path.split() :将路径和文件名分开
import os, sys

path = os.path.split("F:/Proiect/Python-Learning/python/path/demo.py")
print(path)
('F:/Proiect/Python-Learning/python/path', 'demo.py')
6. os.path.join():连接两个或更多的路径名组件
import os

Path1 = 'home'
Path2 = 'develop'
Path3 = 'code'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20)
Path10 =  homedevelopcode
Path20 =  home\develop\code
7. os.path.dirname():去掉文件名,返回目录
import os

path = "F:/Proiect/Python-Learning/python/path/demo.py"
print(os.path.dirname(path))
F:/Proiect/Python-Learning/python/path
8. os.path.basename():返回path最后的文件名。如果path以/或\结尾,那么就会返回空值。
import os

path = "F:/Proiect/Python-Learning/python/path/demo.py"
path1 = os.path.basename(path)
print(path1)
demo.py
9. os.mkdir():只创建一级目录
import os

path = "F:/Proiect/Python-Learning/python/path"
path1 = os.mkdir(path + './file1')
10. os.makedirs():创建多级目录
import os

path = r"F:/Proiect/Python-Learning/python/path" # r 声明其后字符串不需要转义
os.makedirs(path + "./file1"+ "./file1_1" + "./file1_1_1") # 所有关于文件夹的操作文件夹前面要加 ‘./’ 或者 '/'
11. os.remove():删除文件
12. os.chdir(path):用于改变当前工作目录到指定路径
import os
path = './file1'
# 查看当前工作目录
retval = os.getcwd()
print("当前工作目录为:\t", retval)
# 修改当前工作目录
os.chdir(path)
# 查看修改后的工作目录
retval = os.getcwd()
print("目录修改成功:\t", retval)
当前工作目录为:	 F:\Proiect\Python-Learning\python\path
目录修改成功:	 F:\Proiect\Python-Learning\python\path\file1
13. os.path.getmtime(path), os.path.getatime(path), os.path.getctime(path) 查看时间
import os
import time
file = "F:/Proiect/Python-Learning/python/path/demo.py"
Now_time= os.path.getatime(file)   # 输出最近访问时间
print("Now_time:\t", Now_time)
crate_time = os.path.getctime(file)   # windows环境下是输出文件创建时间
print("crate_time:\t", crate_time)
Modify_time = os.path.getmtime(file)   # 输出最近修改时间
print("Modify_time:\t", Modify_time)
time.gmtime(os.path.getmtime(file))    # 以struct_time形式输出最近修改时间
file_size = os.path.getsize(file)    # 输出文件大小
print("file_size:\t", file_size)
abs_path = os.path.abspath(file)    # 输出绝对路径
print("file_size:\t", abs_path)
norm_path = os.path.normpath(file)
print("norm_path:\t", norm_path)
Now_time:	 1605593018.7174108
crate_time:	 1605572830.3529973
Modify_time:	 1605593018.7174108
file_size:	 1174
file_size:	 F:\Proiect\Python-Learning\python\path\demo.py
norm_path:	 F:\Proiect\Python-Learning\python\path\demo.py
14. os.path.exists(path) 、os.path.isfile(path)、os.path.isdir(path)查看文件
import os

path = "F:\Proiect\Python-Learning\python\path\demo.py"
path1 = "F:\Proiect\Python-Learning\python\path"
exists = os.path.exists(path) # 判断path是否存在,存在返回True,不存在返回False
isfile = os.path.isfile(path) # 判断path是否为文件,是返回True,不是返回False
isdir = os.path.isdir(path1) # 判断path是否目录,是返回True, 不是返回False
print("exists:\t", exists)
print("isfile:\t", isfile)
print("isdir:\t", isdir)
exists:	 True
isfile:	 True
isdir:	 True

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值