Skip to main content

Command Palette

Search for a command to run...

Python Python的os包

Published
2 min read
Python Python的os包

Python的os包

os的文件与目录函数介绍

  • import os
函数名参数介绍举例返回值
getcwd返回当前的路径os.getcwd()字符串
listdirpath返回制定路径下所有的文件或文件夹os.listdir('c//Windows')返回一个列表
makedirsPath mode创建多级文件夹os.makedir('d//imooc/py')
removedirspath删除多级文件夹os.removedirs('d://imooc/py')
renameOldname newname给文件或文件夹改名os.rename('d://imooc', 'd//imoc')
rmdirpath只能删除空文件夹os.rmdir('d://imooc')
代码
# coding:utf-8

import os

current_path = os.getcwd()
print(current_path)

new_path = '%s/test1' % current_path
# os.makedirs(new_path)

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

new_path2 = '%s/test2/abc' % current_path
# os.makedirs(new_path2)
# os.makedirs('test3')

# os.removedirs('test2/abc')
# os.rename('test3', 'test3_new')
# os.rename('test1.py', 'python_test1.py')

# os.rmdir('%s/test3_new' % current_path)
os.rmdir('test1')

os.path模块常用函数介绍

函数名参数介绍举例返回值
existsPath文件或路径是否存在os.path.exists('d://')bool类型
isdirPath是否是路径os.path.isdir('d://')bool类型
isabsPath是否是绝对路径os.path.isabs('test')bool类型
isfilePath是否是文件os.path.isfile('d://a.py')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('animal'))

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

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

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

if os.path.exists('test2/abc'):
    os.removedirs('test2/abc')
if os.path.exists('test3'):
    os.rename('test3', 'test3_new')
if os.path.exists('test1.py'):
    os.rename('test1.py', 'python_test1.py')

if os.path.exists('%s/test3_new'):
    os.rmdir('%s/test3_new' % current_path)

if os.path.exists('test1'):
    os.rmdir('test1')
print(dir(os))

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]))

More from this blog

MySQL | 表的内连接

数据操作语言:表连接查询(一) 从多张表中提取数据 从多张表提取数据,必须指定关联的条件。如果不定义关联条件就会出现无条件连接,两张表的数据会交叉连接,产生 笛卡尔积。 规定了连接条件的表连接语句,就不会出现笛卡尔积。 # 查询每名员工的部门信息 SELECT e.empno,e.ename,d.dname FROM t_emp e JOIN t_dept d ON e.deptno=d.deptno; 表连接的分类 表连接分为两种:内连接 和 外连接 内连接是结果集中只保留符合...

May 16, 20221 min read13
MySQL | 表的内连接

MySQL | 分组查询的应用

数据操作语言:分组查询 为什么要分组? 默认情况下汇总函数是对全表范围内的数据做统计 GROUP BY 子句的作用是通过一定的规则将一个数据集划分成若干个小的区域,然后针对每个小区域分别进行数据汇总处理 SELECT deptno,AVG(sal) FROM t_emp GROUP BY deptno; SELECT deptno,ROUND(AVG(sal)) FROM t_emp GROUP BY deptno; -- ROUND 取整 逐级分组 数据库支持多列分组条件,执行的时候...

Apr 27, 20221 min read10
MySQL | 分组查询的应用

MySQL | 聚合函数的使用

数据操作语言:聚合函数 什么是聚合函数 聚合函数在数据的查询分析中,应用十分广泛。聚合函数可以对 数据求和、求 最大值 和 最小值 、求 平均值 等等。 求公司员工的评价月收入是多少? SELECT AVG(sal+IFNULL(comm,0)) FROM t_emp; SELECT AVG(sal+IFNULL(comm,0)) AS avg FROM t_emp; SUM 函数 SUM 函数用于求和,只能用户数字类型,字符类型的统计结果为 0 ,日期类型统计结果是毫秒数相加 SE...

Apr 26, 20221 min read8
MySQL | 聚合函数的使用
U

Untitled Publication

173 posts