Python学习笔记(15)- osos.path 操作文件

程序1

编写一个程序,统计当前目录下每个文件类型的文件数,程序实现如图:

 1 import os
 2 
 3 
 4 def countfile(path):
 5     dict1 = {}  # 定义一个字典
 6     all_files = os.listdir(path)
 7     for each_file in all_files:
 8         if os.path.isdir(os.path.join(path,each_file)):
 9             dict1.setdefault('文件夹', 0)  
10             # setdefault:如果字典中包含有给定键,
11             # 则返回该键对应的值,否则返回为该键设置的值。
12             dict1['文件夹'] += 1  # 参考以前分享的字典修改
13         else:
14             ext = os.path.splitext(each_file)[1]  
15             # 分离文件名与扩展名,返回(f_name, f_extension)元组
16             dict1.setdefault(ext, 0)
17             dict1[ext] += 1
18     #print(dict1)
19     for each_type in dict1.keys():
20         print('该文件夹下共有【%s】类型的文件%d个' 
21               % (each_type, dict1[each_type]))
22 
23 
24 path = input('输入要统计的目录: ')
25 countfile(path)
View Code

程序2

编写一个程序,计算当前文件夹下所有文件的大小,程序实现如图:

 1 import os
 2 
 3 
 4 def count_size(path):
 5     filesize = {}
 6     all_files = os.listdir(path)
 7     for eachfile in all_files:
 8         if os.path.isfile(os.path.join(path, eachfile)):
 9             size = os.path.getsize(os.path.join(path, eachfile))
10             filesize[eachfile] = size
11             #filesize.setdefault(eachfile, os.path.getsize(os.path.join(path, eachfile)))
12 
13     for each in filesize.items():
14         print("%s【%sBytes】" % (each[0], each[1]))
15 
16 
17 path = input('输入要统计的目录: ')
18 count_size(path)
View Code

os模块中关于文件/目录常用的函数使用方法

函数名

使用方法

getcwd()

返回当前工作目录

chdir(path)

改变工作目录

listdir(path='.')

列举指定目录中的文件名('.'表示当前目录,'..'表示上一级目录)

mkdir(path)

创建单层目录,如该目录已存在抛出异常

makedirs(path)

递归创建多层目录,如该目录已存在抛出异常,注意:'E:\a\b'和'E:\a\c'并不会冲突

remove(path)

删除文件

rmdir(path)

删除单层目录,如该目录非空则抛出异常

removedirs(path)

递归删除目录,从子目录到父目录逐层尝试删除,遇到目录非空则抛出异常

rename(old, new)

将文件old重命名为new

system(command)

运行系统的shell命令

walk(top)

遍历top路径以下所有的子目录,返回一个三元组:(路径, [包含目录], [包含文件])【具体实现方案请看:第30讲课后作业^_^】

以下是支持路径操作中常用到的一些定义,支持所有平台

os.curdir

指代当前目录('.')

os.pardir

指代上一级目录('..')

os.sep

输出操作系统特定的路径分隔符(Win下为'\',Linux下为'/')

os.linesep

当前平台使用的行终止符(Win下为' ',Linux下为' ')

os.name

指代当前使用的操作系统(包括:'posix', 'nt', 'mac', 'os2', 'ce', 'java')

os.path模块中关于路径常用的函数使用方法

函数名

使用方法

basename(path)

去掉目录路径,单独返回文件名

dirname(path)

去掉文件名,单独返回目录路径

join(path1[, path2[, ...]])

将path1, path2各部分组合成一个路径名

split(path)

分割文件名与路径,返回(f_path, f_name)元组。如果完全使用目录,它也会将最后一个目录作为文件名分离,且不会判断文件或者目录是否存在

splitext(path)

分离文件名与扩展名,返回(f_name, f_extension)元组

getsize(file)

返回指定文件的尺寸,单位是字节

getatime(file)

返回指定文件最近的访问时间(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)

getctime(file)

返回指定文件的创建时间(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)

getmtime(file)

返回指定文件最新的修改时间(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)

以下为函数返回 True 或 False

exists(path)

判断指定路径(目录或文件)是否存在

isabs(path)

判断指定路径是否为绝对路径

isdir(path)

判断指定路径是否存在且是一个目录

isfile(path)

判断指定路径是否存在且是一个文件

islink(path)

判断指定路径是否存在且是一个符号链接

ismount(path)

判断指定路径是否存在且是一个挂载点

samefile(path1, paht2)

判断path1和path2两个路径是否指向同一个文件

张老师提供咨询服务啦-->点击查看 <--

往期回顾

Python学习笔记(1)-介绍

Python学习笔记(2)

Python学习笔记(3)-数据类型

Python学习笔记(4)-循环

Python学习笔记(5)-循环

Python学习笔记(6)-循环

Python学习笔记(7)

Python学习笔记(8)-四个小程序

Python学习笔记(9)-函数

Python学习笔记(10)-回文联和统计入参

Python学习笔记(11)-递归

Python学习笔记(12)-字典

Python学习笔记(13)-集合

Python学习笔记(14)-文件

Python学习笔记(15)-文件

原文地址:https://www.cnblogs.com/scios/p/8309955.html