python学习[第十五篇] 文件系统
python学习[第十五篇] 文件系统
对文件系统访问大多数都通过os模块实现。
os 模块文件/目录访问函数
文件处理
mkfifo() 创建命名通道只用于linux
remove(path)/unlink(path)删除文件
rename()/renames() 重命名文件
stat() 返回文件信息
symlink() 创建连接,只用于linux
utime() 更新文件时间戳
walk() 生成一个目录下的所有文件
#remove >>> os.remove("D:/cmdargs.py") #rename >>> os.renames('D:/ryan/ryan.txt','D:/ryan2/ryan2.txt') #stat >>> os.stat('D:/ryan.txt') nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid =0, st_size=152L, st_atime=1532525071L, st_mtime=1532525088L, st_ctime=153252507 1L) #utime >>> os.utime('D:/ryan2/ryan2.txt',None) #walk >>> x=os.walk('D:/ryan2') >>> for y in x: ... print y ... ('D:/ryan2', [], ['ryan2.txt']) #tmpfile >>> f=os.tmpfile() Traceback (most recent call last): File "<stdin>", line 1, in <module> OSError: [Errno 13] Permission denied
目录/文件夹
getcwd() /getcwdu() 返回当前工作目录/返回当前目录,以unicode字符串的形式
listdir() 列出指定目录的文件
mkdir()/makedirs() 创建目录/创建多层目录
rmdir() /removedirs() 删除目录/删除多层目录
chdir() 改变当前目录
chroot()改变当前进程的根目录 linux /unix
>>> os.getcwd() 'D:\\ryan2' >>> os.getcwdu() u'D:\\ryan2' >>> os.chdir('d:/ryan') #chdir 目录不存在的话,会报WindowsError >>> os.chdir('d:/ryan3') Traceback (most recent call last): File "<stdin>", line 1, in <module> WindowsError: [Error 2] : 'd:/ryan3' >>> os.listdir('c:/') ['$Recycle.Bin', 'AMD', 'BaiduNetdiskDownload', 'bef7a4d9c1ea713f1cedd1669f7c7a08', 'comments.txt', 'cookie.txt', 'Documents and Settings', 'DRIVERS', 'DRMsoft' , 'hiberfil.sys', 'Intel', 'jdk', 'MyDrivers', 'pagefile.sys', 'PerfLogs', 'Program Files', 'Program Files (x86)', 'ProgramData', 'Python27', 'Python37', 'QMDownload', 'qqpcmgr_docpro', 'Recovery', 'sqlite', 'Sun', 'System Volume Informatio n', 'test.py', 'Users', 'vs2013', 'Windows', '\xd0\xc2\xbd\xa8\xce\xc4\xbc\xfe\xbc\xd0', '\xd1\xb8\xc0\xd7\xcf\xc2\xd4\xd8'] >>> os.makedirs('D:/ryan2/ryan2/ryan2') >>> os.mkdir('ryan2') >>> os.rmdir('ryan2') >>> os.removedirs('D:/ryan/ryan2')
访问权限
access() 检验权限模式
chmod() 改变权限模式
chown() 修改owner和goroupID
文件描述符专用
open()
read()/write()
dup/dup2()
os.path 模块中路径名访问函数
分隔
basename(path)去掉目录路径返回文件名
dirname(path) 去掉文件名,返回目录路径
join(path,path2) 将分离的各部分组成一个路径名
split(path) 返(dirname,basename) 元组
splitdirve(path) 返回 (drivename,pathname)元组
splittext(path) 返回(filename,extension)元组
>>> os.path.basename('c:/ryan2/test.py') 'test.py' >>> os.path.dirname('c:/ryan2/test.py') 'c:/ryan2' >>> os.path.split('c:/ryan2/test.py') ('c:/ryan2', 'test.py') >>> os.path.splitext('c:/ryan2/test.py') ('c:/ryan2/test', '.py') >>> os.path.splitdrive('c:/ryan2/test.py') ('c:', '/ryan2/test.py') >>> os.path.join('c:/foo/','test.txt') 'c:/foo/test.txt'
信息
getatime(path) 返回最近访问时间
getctime(path) 返回文件创建时间
getmtime(path)返回文件修改时间
getsize(path) 返回文件大小
>>> os.path.getatime('ryan.txt') 1532525145.1479454 >>> os.path.getctime('ryan.txt') 1532525145.1479454 >>> os.path.getmtime('ryan.txt') 1532525088.2246895 >>> os.path.getsize('ryan.txt') 152L
查询
exists(path) 判断文件或目录是否存在
isabs(path) 判断是否是绝对路径
isdir(path) 判断是否是目录
isfile(path) 判读是否是文件
islink(path) 判断是否是连接
ismount(path) 判断是否是挂载点
samefile(path1,path2) 是否是同样的文件 unix
>>> os.path.exists('ryan.txt') True >>> os.path.exists('test') True >>> os.path.isabs('test') False >>> os.path.isdir('test') True >>> os.path.isdir('test1') False >>> os.path.isfile('test') False >>> os.path.isfile('ryan.txt') True >>> os.path.islink('ryan.txt') False >>> os.path.ismount('c:') False >>> os.path.samefile('ryan.txt','ryan2.txt') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'samefile'
###end