美文网首页
Python3 OS 文件/目录方法

Python3 OS 文件/目录方法

作者: crunch114 | 来源:发表于2019-12-24 22:01 被阅读0次

OS

import 模块、包

import os.path as osp
import sys
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))

把上级目录加到sys.path中

操作文件和目录

操作文件和目录的函数一部分放在os模块中,一部分放在os.path模块中
菜鸟教程

#***获取当前目录***
import os
os.getcwd()
#***获取上级目录***
os.path.abspath(os.path.dirname(os.getcwd()))
#或者
#os.path.abspath(os.path.join(os.getcwd(), ".."))
#***获取上上级目录***
os.path.abspath(os.path.join(os.getcwd(), "../.."))
#***退到上级目录***
path=os.path.abspath(os.path.dirname(os.getcwd()))
os.chdir(path)
#***新建目录***
if not os.path.exists(save_dir):
  os.makedirs(save_dir)


os.chdir(path)
改变当前工作目录
os.listdir(path)
返回path指定的文件夹包含的文件或文件夹的名字的列表。
os.open(file, flags[, mode])
打开一个文件,并且设置需要的打开选项,mode参数是可选的
os.path.join(path1[, path2[, ...]])
把目录和文件名合成一个路径
parent_path = os.path.dirname(file_path)
file_name = os.path.split(file_path)[-1]
获取文件所在目录和文件名

常搭配字符串使用
Python3 字符串
startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。
如果参数 beg 和 end 指定值,则在指定范围内检查。
str.startswith(substr, beg=0,end=len(string))

image.png

Python 文件I/O 读写

https://www.runoob.com/python/python-files-io.html

Python3 os.path() 模块

image.png

相关文章

  • python创建与删除文件

    Python3 OS 文件/目录方法常用的方法如下: os.access(path, mode)检验权限模式 os...

  • Python | os

    文件和目录的处理 更多请看:菜鸟教程 | Python3 OS 文件/目录方法[https://m.runoob....

  • Python3 OS 文件/目录方法

    os 模块提供了非常丰富的方法用来处理文件和目录。常用的方法如下表所示:

  • Python3 OS文件/目录方法

    本文章转自越努力越幸运_2528 os模块提供了非常丰富的方法用来处理文件和目录。常用的方法如下表所示: 1 os...

  • Python3 OS 文件/目录方法

    操作文件和目录 操作文件和目录的函数一部分放在os模块中,一部分放在os.path模块中菜鸟教程 os.chdir...

  • Python3 OS 文件/目录方法

    *图片字体较小,烦请点开图片并观看学习 好了,本文就给大伙分享到这里,文末分享一波福利 获取方式:加python群...

  • python os模块示例总结

    os 常用方法os.remove() 删除文件os.rename() 重命名文件os.walk() 生成目录树下的...

  • Python os.walk() 方法

    Python os.walk() 方法 概述 os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,...

  • Python3入门(十一)OS文件/目录方法

    os模块提供了非常丰富的方法用来处理文件和目录。常用的方法如下表所示 本文到此结束

  • os常用方法

    os的一些方法 os.path.abspath(__file__) 文件目录名(包含文件名) os.path.di...

网友评论

      本文标题:Python3 OS 文件/目录方法

      本文链接:https://www.haomeiwen.com/subject/mvmfoctx.html