python基础之文件总结(二)

众所周知,python是一个跨平台的语言,源代码可以在不同的系统平台执行。关于文件、目录的一些操作,有两个特别重要的模块,os模块和os.path模块。

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

关于工作目录的操作
getcwd()返回当前工作目录

>>> import os
>>> os.getcwd()
'D:\\ruanjian\\Anaconda\\Scripts'

chrcwd()改变工作目录

>>> os.chdir('D:\\')
>>> os.getcwd()
'D:\\'

关于目录的操作
listdir(绝对路径),列出目录里的文件。

>>> os.listdir('D:\\youxiv\\英雄联盟')
['Cross', 'LeagueClient', 'TCLS']

mkdir(path),创建单层目录。若目录已存在,则抛出异常。

>>> os.mkdir('D:\\new')
>>> os.mkdir('D:\\new')
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    os.mkdir('D:\\new')
FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。: 'D:\\new'

makedirs(path),递归创建多层目录。若目录已存在,则抛出异常。

>>> os.makedirs('D:\\A\\B\\C')

rmdir(path),删除单层目录,若目录非空,则抛出异常。
removedirs(path),递归删除多层目录。若目录非空,则抛出异常。

>>> os.rmdir('D:\\new')
>>> os.removedirs('D:\\A\\B\\C')

关于文件的操作
remove(path),删除文件
rename(old,new),重命名文件

>>> os.rename('D:\\A\\B\\C\\new.txt','D:\\A\\B\\C\\rename.txt')

>>> os.remove('D:\\A\\B\\C\\rename.txt')

其他
system(command),运行系统的shell命令
walk(top),遍历top目录下的所有子目录,返回一个三元组:(路径, [包含目录], [包含文件])
一些常用定义,支持所有平台。

os.curdir
指代当前目录(’.’)
os.pardir
指代上一级目录(’..’)
os.sep
输出操作系统特定的路径分隔符(Win下为’\’,Linux下为’/’)
os.linesep
当前平台使用的行终止符(Win下为’\r\n’,Linux下为’\n’)
os.name
指代当前使用的操作系统(包括:’posix’, ‘nt’, ‘mac’, ‘os2’, ‘ce’, ‘java’)

os.path模块中关于路径常用的函数使用方法
获取路径文件名和目录
basename(path),返回目标路径中去掉目录的文件名
dirname(path),返回目标路径中去掉文件名的目录

>>> import os.path as osp
>>> osp.basename('D:\\A\\B\\C\\new.txt')
'new.txt'
>>> osp.dirname('D:\\A\\B\\C\\new.txt')
'D:\\A\\B\\C'

路径拼接和分离
join(path1,path2),将path1和path2各部分组合成一个路径
split(path),将path的目录和文件名分离,返回返回(f_path, f_name)元组
splitext(path),将path的文件名和扩展名分离,返回(f_name, f_extension)元组

>>> osp.join('D:\\A\\B\\C\\','.\\new.txt')

'D:\\A\\B\\C\\.\\new.txt'
>>> osp.split('D:\\A\\B\\C\\.\\new.txt')

('D:\\A\\B\\C\\.', 'new.txt')
>>> osp.splitext('D:\\A\\B\\C\\.\\new.txt')

('D:\\A\\B\\C\\.\\new', '.txt')

获取文件属性信息
getsize(file)返回指定文件的尺寸,单位是字节
getatime(file)返回指定文件最近的访问时间(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)
getctime(file)返回指定文件的创建时间(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)
getmtime(file)返回指定文件最新的修改时间(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)

>>> osp.getsize('D:\\new_file.txt')

92
>>> osp.getatime('D:\\new_file.txt')

1534310535.8225493


>>> import time
>>> ctime =osp.getctime('D:\\new_file.txt')

>>> ctime

1534305307.3186302
>>> localtiome =time.localtime(ctime)

>>> localtiome

time.struct_time(tm_year=2018, tm_mon=8, tm_mday=15, tm_hour=11, tm_min=55, tm_sec=7, tm_wday=2, tm_yday=227, tm_isdst=0)
>>> mtime =osp.getmtime('D:\\new_file.txt')

>>> mtime

1534307334.6686945

对路径进行判断

exists(path)判断指定路径(目录或文件)是否存在
isabs(path)判断指定路径是否为绝对路径
isdir(path)判断指定路径是否存在且是一个目录
isfile(path)判断指定路径是否存在且是一个文件
islink(path)判断指定路径是否存在且是一个符号链接
ismount(path)判断指定路径是否存在且是一个挂载点
samefile(path1, paht2)判断path1和path2两个路径是否指向同一个文件

>>> osp.exists('D:\\new_file.txt')

True
>>> osp.isabs('D:\\new_file.txt')

True
>>> osp.isabs('new_file.txt')

False
>>> osp.isdir('D:\\new_file.txt')

False
>>> osp.isdir('D:')

True
>>> osp.isfile('D:\\new_file.txt')

True
>>> osp.islink('D:\\new_file.txt')

False

>>> osp.ismount('D:\\')

True
>>> osp.samefile('D:\\new_file.txt','D:\\')

False
>>> 

猜你喜欢

转载自blog.csdn.net/weixin_42920648/article/details/81706187