当前位置: 首页 > news >正文

Python语言零基础入门教程(二十七)

Python OS 文件/目录方法

Python语言零基础入门教程(二十六)

61、Python os.utime() 方法

概述
os.utime() 方法用于设置指定路径文件最后的修改和访问时间。

在Unix,Windows中有效。

语法
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_mode=33188, st_ino=3940649674337682L, st_dev=277923425L, st
_nlink=1, st_uid=400, st_gid=401, st_size=335L, st_atime=1330498070, st_mtime=13
30498074, st_ctime=1330498065)
a2.py 的访问时间: 1330498070
a2.py 的修改时间: 1330498074
done!!

62、Python os.walk() 方法

概述
os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下。

os.walk() 方法是一个简单易用的文件、目录遍历器,可以帮助我们高效的处理文件、目录方面的事情。

在Unix,Windows中有效。

语法
walk()方法语法格式如下:

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])

参数
top – 是你所要遍历的目录的地址, 返回的是一个三元组(root,dirs,files)。
root 所指的是当前正在遍历的这个文件夹的本身的地址
dirs 是一个 list ,内容是该文件夹中所有的目录的名字(不包括子目录)
files 同样是 list , 内容是该文件夹中所有的文件(不包括子目录)
topdown --可选,为 True,则优先遍历 top 目录,否则优先遍历 top 的子目录(默认为开启)。如果 topdown 参数为 True,walk 会遍历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(".", topdown=False):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.py

63、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_year=2018, tm_mon=10, tm_mday=9, tm_hour=2, tm_min=40, tm_sec=5, tm_wday=1, tm_yday=282, tm_isdst=0)
7
/root/runoob.txt
/root/runoob.txt
http://www.lryc.cn/news/12981.html

相关文章:

  • Redis基础操作以及数据类型
  • 自抗扰控制ADRC之反馈控制律(NLSEF)
  • “生成音乐“ 【循环神经网络】
  • 能否手写vue3响应式原理-面试进阶
  • 前端工程师leetcode算法面试必备-简单的二叉树
  • 【什么程度叫熟悉linux系统】
  • 编译安装MySQL
  • Kubernetes一 Kubernetes之入门
  • SQLServer2000 断电后数据库suspect“置疑”处理
  • 多模态机器学习入门Tutorial on MultiModal Machine Learning——第一堂课个人学习内容
  • Java ~ Collection/Executor ~ LinkedBlockingDeque【总结】
  • .NET7的AOT的使用
  • 分布式缓存的问题
  • golang入门笔记——内存管理和编译器优化
  • GEE学习笔记 七十:【GEE之Python版教程四】Python基础编程二
  • 股票投资新出发之知识体系构建导论
  • 蓝桥杯算法训练合集 十六 1.首字母变大写2.盾神计科导作业3.Cinema4.接水问题
  • 密码的世界
  • 如何用一句话感动测试工程师?产品和技术都这么说!
  • 3|物联网控制|计算机控制-刘川来胡乃平版|第2章:计算机控制系统中的检测设备和执行机构-2.1传感器和变送器|课堂笔记|ppt
  • MySQL中使用索引优化
  • Linux C/C++ 多线程TCP/UDP服务器 (监控系统状态)
  • 【JavaScript】JavaScript基本使用方法
  • Python数据容器、list列表、tuple元组、str字符串、数据容器(序列)切片、set集合、dict字典、字符串大小比较
  • Python urllib
  • Centos7安装Python3
  • [U3D ShaderGraph] 全面学习ShaderGraph节点 | 第四课 | Input/Lighting
  • SpringBoot升级到3.0
  • JavaWeb8-线程安全问题
  • 进程切换-