python3路径操作 os

import os
import shutil

os.getcwd()  # 获取当前工作目录,非脚本目录
os.listdir()  # 返回指定目录下的所有文件和目录,非递归
os.remove()  # 删除文件
os.removedirs()  #删除空目录,删除非空目录请使用shutil.rmtree('dir')
os.path.isfile()  # 检验给出的路径是否是一个文件
os.path.isdir()  # 检验给出的路径是否是一个目录
os.path.isabs()  # 判断是否是绝对路径
os.path.exists()  # 检验给出的路径是否真实存在
os.path.split()  # 返回一个路径的目录名和文件名
os.path.splitext()  # 分离文件扩展名
os.path.dirname()  # 获取文件路径名
os.path.basename() # 获取一个绝对路径下的文件名
os.path.abspath(__file__) #返回当前.py文件的绝对路径  以xxx.py结尾
os.path.dirname(__file__) #返回当前.py文件的目录   d:/xxx  对比 os.getcwd()  d:\xxx
os.path.join("home", "me", "mywork")  # 路径连接  'home/me/mywork'
os.path.getsize(filename)  # 获取文件大小
parent_dir = os.path.abspath('..')    #返回上级目录,与parent_dir = os.path.abspath(os.path.pardir)等效
current_dir = os.path.abspath('.')    #返回当前目录,与 current_dir = os.path.dirname(__file__)等效
os.system()  # 运行shell命令
os.rename(old,new) # 重命名文件或目录
os.makedirs(r"c:\python\test")  # 创建多级目录
os.mkdir("test")  # 创建单个目录
os.exit()  # 终止当前进程
os.mknod("test.txt")  # 创建空文件
os.listdir('/path')   #查看该目录中所有文件及文件夹,返回list

shutil.copyfile("oldfile","newfile")  # oldfile和newfile都只能是文件
shutil.copytree("olddir","newdir")  # olddir和newdir都只能是目录,且newdir必须不存在
shutil.move("oldpos","newpos")  # 移动文件或目录
shutil.rmtree("dir")  # 删除目录,可以是非空目录

作者:milletluo
来源:CSDN
原文:https://blog.csdn.net/lm409/article/details/75452306

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值