阿里网站怎么建设,上海性价比高的装修公司,wordpress博客搭建,先申请域名后做网站Python OS 文件/目录方法
Python语言零基础入门教程#xff08;二十六#xff09;
61、Python os.utime() 方法
概述 os.utime() 方法用于设置指定路径文件最后的修改和访问时间。
在Unix#xff0c;Windows中有效。
语法 utime()方法语法格式如下#xff1a;
os.uti…Python OS 文件/目录方法
Python语言零基础入门教程二十六
61、Python os.utime() 方法
概述 os.utime() 方法用于设置指定路径文件最后的修改和访问时间。
在UnixWindows中有效。
语法 utime()方法语法格式如下
os.utime(path, times)参数 path – 文件路径
times – 如果时间是 None, 则文件的访问和修改设为当前时间 。 否则, 时间是一个 2-tuple数字, (atime, mtime) 用来分别作为访问和修改的时间。
返回值 该方法没有返回值。
实例 以下实例演示了 utime() 方法的使用
#!/usr/bin/python
# -*- coding: UTF-8 -*-import os, sys# 显示文件的 stat 信息
stinfo os.stat(a2.py)
print stinfo# 使用 os.stat 来接收文件的访问和修改时间
print a2.py 的访问时间: %s %stinfo.st_atime
print a2.py 的修改时间: %s %stinfo.st_mtime# 修改访问和修改时间
os.utime(a2.py,(1330712280, 1330712292))
print done!!执行以上程序输出结果为
posix.stat_result(st_mode33188, st_ino3940649674337682L, st_dev277923425L, st
_nlink1, st_uid400, st_gid401, st_size335L, st_atime1330498070, st_mtime13
30498074, st_ctime1330498065)
a2.py 的访问时间: 1330498070
a2.py 的修改时间: 1330498074
done!!62、Python os.walk() 方法
概述 os.walk() 方法用于通过在目录树中游走输出在目录中的文件名向上或者向下。
os.walk() 方法是一个简单易用的文件、目录遍历器可以帮助我们高效的处理文件、目录方面的事情。
在UnixWindows中有效。
语法 walk()方法语法格式如下
os.walk(top[, topdownTrue[, onerrorNone[, followlinksFalse]]])参数 top – 是你所要遍历的目录的地址, 返回的是一个三元组(root,dirs,files)。 root 所指的是当前正在遍历的这个文件夹的本身的地址 dirs 是一个 list 内容是该文件夹中所有的目录的名字(不包括子目录) files 同样是 list , 内容是该文件夹中所有的文件(不包括子目录) topdown --可选为 True则优先遍历 top 目录否则优先遍历 top 的子目录(默认为开启)。如果 topdown 参数为 Truewalk 会遍历top文件夹与top 文件夹中每一个子目录。 onerror – 可选需要一个 callable 对象当 walk 需要异常时会调用。 followlinks – 可选如果为 True则会遍历目录下的快捷方式(linux 下是软连接 symbolic link )实际所指的目录(默认关闭)如果为 False则优先遍历 top 的子目录。
返回值 返回生成器。
实例 以下实例演示了 walk() 方法的使用
#!/usr/bin/python
# -*- coding: UTF-8 -*-import os
for root, dirs, files in os.walk(., topdownFalse):for name in files:print(os.path.join(root, name))for name in dirs:print(os.path.join(root, name))执行以上程序输出结果为
./.bash_logout
./amrood.tar.gz
./.emacs
./httpd.conf
./www.tar.gz
./mysql.tar.gz
./test.py
./.bashrc
./.bash_history
./.bash_profile
./tmp
./tmp/test.py63、Python os.write() 方法
概述 os.write() 方法用于写入字符串到文件描述符 fd 中. 返回实际写入的字符串长度。
在Unix中有效。
语法 write()方法语法格式如下
os.write(fd, str)参数 fd – 文件描述符。
str – 写入的字符串。
返回值 该方法返回写入的实际位数。
实例 以下实例演示了 write() 方法的使用
#!/usr/bin/python
# -*- coding: UTF-8 -*-import os, sys# 打开文件
fd os.open(f1.txt,os.O_RDWR|os.O_CREAT)# 写入字符串
ret os.write(fd,This is runoob.com site)# 输入**返回值**
print 写入的位数为:
print retprint 写入成功# 关闭文件
os.close(fd)
print 关闭文件成功!!执行以上程序输出结果为
写入的位数为:
23
写入成功
关闭文件成功!!64、Python os.path 模块
os.path 模块主要用于获取文件的属性。 以下是 os.path 模块的几种常用方法 实例 以下实例演示了 os.path 相关方法的使用
#!/usr/bin/python
# -*- coding: UTF-8 -*-import osprint( os.path.basename(/root/runoob.txt) ) # 返回文件名
print( os.path.dirname(/root/runoob.txt) ) # 返回目录路径
print( os.path.split(/root/runoob.txt) ) # 分割文件名与路径
print( os.path.join(root,test,runoob.txt) ) # 将目录和文件名合成一个路径执行以上程序输出结果为
runoob.txt
/root
(/root, runoob.txt)
root/test/runoob.txt以下实例输出文件的相关信息。
#!/usr/bin/python
# -*- coding: UTF-8 -*-import os
import timefile/root/runoob.txt # 文件路径print( os.path.getatime(file) ) # 输出最近访问时间
print( os.path.getctime(file) ) # 输出文件创建时间
print( os.path.getmtime(file) ) # 输出最近修改时间
print( time.gmtime(os.path.getmtime(file)) ) # 以struct_time形式输出最近修改时间
print( os.path.getsize(file) ) # 输出文件大小字节为单位
print( os.path.abspath(file) ) # 输出绝对路径
print( os.path.normpath(file) ) # 规范path字符串形式执行以上程序输出结果为
1539052805.5735736
1539052805.5775735
1539052805.5735736
time.struct_time(tm_year2018, tm_mon10, tm_mday9, tm_hour2, tm_min40, tm_sec5, tm_wday1, tm_yday282, tm_isdst0)
7
/root/runoob.txt
/root/runoob.txt