python的os模块

一、os模块

1、os的文件与目录函数

函数名参数介绍举例返回值
getcwd返回当前的路径os.getcwd()字符串
listdirpath

返回指定路径下所有的文件或文件夹

os.listdir('c://abc')返回一个列表
makedirspathmode创建多级文件夹os.makedirs('d://abc/ab')
removedirspath删除多级文件夹os.removedirs('d://abc/ab')
rename

Oldname

Newname

给文件或文件夹改名os.rename('d://abc',‘d://www’)
rmdirpath只能删除空文件夹os.rmdir('d://abc')

2、os.path模块常用方法

函数名参数介绍举例返回值
existspath文件或路径是否存在os.path.exists('d://a')bool
isdirpath是否是路径os.path.isdir("d://")bool
isabspath是否为绝对路径os.path.isabs('test')bool
isfilepath是否为文件os.path.isfile('d://a.txt')bool
joinpath,path*路径字符串合并os.path.join('d://','test')字符串
splitpath以最后一层路径为基准切割os.path.split('d://test')列表

以上方法相关的测试代码如下

# coding:utf-8

import os
import os.path


current_path = os.getcwd()
print(current_path)
print(os.path.isabs(current_path))
print(os.path.isabs('abc'))



new_path = '%s/test1' % current_path
if os.path.exists(new_path):
    os.makedirs(new_path)

data = os.listdir(current_path)
print(data)

new_path1 = os.path.join(current_path, 'test1', 'abc')
print(new_path1)
if os.path.exists(new_path1):
    os.makedirs(new_path1)
if os.path.exists('test2'):
    os.makedirs('test2')

if os.path.exists('test1/abc'):
    os.removedirs('test1/abc')
if os.path.exists('test2'):
    os.rename('test2', 'test2_new')
if os.path.exists('demo1.py'):
    os.rename('demo1.py', 'demo2.py')
if os.path.exists('%s/test1_new' % current_path):
    os.rmdir('%s/test1_new' % current_path)
if os.path.exists('test1'):
    os.rmdir('test1')

path1=os.path.join(current_path,'123')
print(path1)
os.mkdir(path1)

current_path = current_path + '/package_os.py'
print(os.path.isfile(current_path))
print(os.path.split(current_path))
print(os.path.isdir(os.path.split(current_path)[0]))
print(dir(os.path))



 

Pythonos模块是一个用于处理文件系统的常用模块。它提供了各种方法来处理文件和目录。在Python中,os模块是跨平台的,这意味着你不需要关心操作系统使用了什么模块os模块会自动选择并调用正确的模块。 通过使用os模块,你可以轻松地获取当前工作目录(即当前Python脚本所在的目录)的路径,使用`os.getcwd()`方法即可实现。 此外,os模块还提供了许多其他有用的方法,例如`os.listdir()`方法可以列出指定路径下的所有文件和目录。你只需要提供一个路径作为参数,比如`os.listdir("C:Users黄伟Desktoppublishos模块 est_os模块")`就可以获取该路径下的所有文件和目录列表。 总结来说,Pythonos模块是一个十分有用的模块,它提供了处理文件系统的功能,包括获取当前工作目录和列出指定路径下的文件和目录等功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python OS 模块](https://blog.csdn.net/hj1993/article/details/129431726)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python模块——os模块详解](https://blog.csdn.net/qq_46416934/article/details/125241212)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值