Python文件及目录操作实例详解


Posted in Python onJune 04, 2015

本文实例讲述了Python文件及目录操作的方法。分享给大家供大家参考。具体分析如下:

在python中对文件及目录的操作一般涉及多os模块,os.path模块。具体函数以及使用方法在程序中说明。

#!/usr/bin/env python
#-*- coding=UTF8 -*-
import os
import os.path as op
def change_dir():
  '''
 该函数显示及改变前目录
 using chdir() to change current dir
    getcwd() can show the current working directory
  '''
  directory="/tmp"
  #使用getcwd()返回当前目录
  print os.getcwd()
  #chdir改变当前目录为:directory目录
  os.chdir(directory)
  print os.getcwd()
def show_filesOfdir(whichDir):
  '''
 此函数只显示目录下的所有文件
 using listdir() to shows all of the file execpt directory
   join() function catenate 'whichDir' with listdir() returns values
   isfile() check that file is a regular file
   '''  
   #listdir() 函数显示前目录的内容
  for file in os.listdir(whichDir):
 #利用join()把whichDir目录及listdir() 返回值连接起来组成合法路径
    file_name = op.join(whichDir,file)
 #isfile()函数可以判断该路径上的文件是否为一个普通文件
    if op.isfile(file_name):
      print file_name
def printaccess(path):
  ''' 
 显示文件的最后访问时间,修改时间
 shows 'path' the last access time 
      getatime() return the time of last access of path
   stat() return information of a file,use its st_atime return the time of last access
   ctime() return a string of local time
  '''
  import time
  #利用ctime()函数返回最后访问时间
  #getatime()函数返回最后访问时间,不过是以秒为单位(从新纪元起计算)
  print time.ctime(op.getatime(path))
  #stat()函数返回一个对象包含文件的信息
  stat = os.stat(path)
  #st_atime 最后一次访问的时间
  print time.ctime(stat.st_atime)
  print the modify time
  print "modify time is:",
  print time.ctime(op.getctime(path))
  print "modify time is:",
  #st_ctime 最后一次修改的时间
  print time.ctime(stat.st_ctime)
def isDIR(path):
  '''
 一个os.path.isdir()函数的实现
 Implement isdir() function by myself
  '''
  import stat
  MODE = os.stat(path).st_mode
  #返回真假值
  return stat.S_ISDIR(MODE)
if __name__== "__main__":
  change_dir()
  show_filesOfdir('''/root''')
  printaccess('/etc/passwd')
  print isDIR('/etc')

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python格式化css文件的方法
Mar 10 Python
python字符串连接方法分析
Apr 12 Python
Pyhton中单行和多行注释的使用方法及规范
Oct 11 Python
python fabric实现远程部署
Jan 05 Python
python实现手机通讯录搜索功能
Feb 22 Python
Python基于递归实现电话号码映射功能示例
Apr 13 Python
flask框架使用orm连接数据库的方法示例
Jul 16 Python
python如何生成各种随机分布图
Aug 27 Python
CentOS 7 安装python3.7.1的方法及注意事项
Nov 01 Python
python消除序列的重复值并保持顺序不变的实例
Nov 08 Python
pyqt5 QScrollArea设置在自定义侧(任何位置)
Sep 25 Python
Anaconda安装pytorch和paddle的方法步骤
Apr 03 Python
Python通过poll实现异步IO的方法
Jun 04 #Python
Python通过select实现异步IO的方法
Jun 04 #Python
Python守护进程用法实例分析
Jun 04 #Python
Python使用multiprocessing创建进程的方法
Jun 04 #Python
python在windows下创建隐藏窗口子进程的方法
Jun 04 #Python
python实现支持目录FTP上传下载文件的方法
Jun 03 #Python
python实现的DES加密算法和3DES加密算法实例
Jun 03 #Python
You might like
dede3.1分页文字采集过滤规则详说(图文教程)续二
2007/04/03 PHP
php实现信用卡校验位算法THE LUHN MOD-10示例
2014/05/07 PHP
php通过array_unshift函数添加多个变量到数组前端的方法
2015/03/18 PHP
PHP的Yii框架中Model模型的学习教程
2016/03/29 PHP
Thinkphp5 微信公众号token验证不成功的原因及解决方法
2017/11/12 PHP
prototype.js的Ajax对象
2006/09/23 Javascript
JQuery中的$.getJSON 使用说明
2011/03/10 Javascript
JQuery扩展插件Validate—6 radio、checkbox、select的验证
2011/09/05 Javascript
javascript控制在光标位置插入文字适合表情的插入
2014/06/09 Javascript
jquery实现表格本地排序的方法
2015/03/11 Javascript
javascript解三阶幻方(九宫格)
2015/04/22 Javascript
jQuery简单实现验证邮箱格式
2015/07/15 Javascript
pace.js页面加载进度条插件
2015/09/29 Javascript
JavaScript中SetInterval与setTimeout的用法详解
2015/11/10 Javascript
jQuery 移动端artEditor富文本编辑器
2016/01/11 Javascript
Bootstrap轮播插件简单使用方法介绍
2016/06/21 Javascript
jQuery plugin animsition使用小结
2017/09/14 jQuery
vue组件实现进度条效果
2018/06/06 Javascript
原生JS实现留言板功能
2020/02/08 Javascript
[01:17:55]VGJ.T vs Mineski 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/20 DOTA
详解Python中的条件判断语句
2015/05/14 Python
编写Python小程序来统计测试脚本的关键字
2016/03/12 Python
python实现获取单向链表倒数第k个结点的值示例
2019/10/24 Python
jupyter 使用Pillow包显示图像时inline显示方式
2020/04/24 Python
html5教程画矩形代码分享
2013/12/04 HTML / CSS
Joe Fresh官网:加拿大时尚品牌和零售连锁店
2016/11/30 全球购物
携程英文网站:Trip.com
2017/02/07 全球购物
C&A巴西网上商店:时尚、衣服、手机和鞋子
2020/06/07 全球购物
浅谈react路由传参的几种方式
2021/03/23 Javascript
办公室助理岗位职责
2013/12/25 职场文书
法务专员岗位职责
2014/01/02 职场文书
设计专业毕业生求职信
2014/06/25 职场文书
小学中队委竞选稿
2015/11/20 职场文书
Flask response响应的具体使用
2021/07/15 Python
Python学习之迭代器详解
2022/04/01 Python
python微信智能AI机器人实现多种支付方式
2022/04/12 Python