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多线程编程(四):使用Lock互斥锁
Apr 05 Python
在Python中使用CasperJS获取JS渲染生成的HTML内容的教程
Apr 09 Python
Python进程间通信用法实例
Jun 04 Python
Python下的Softmax回归函数的实现方法(推荐)
Jan 26 Python
Python实现改变与矩形橡胶的线条的颜色代码示例
Jan 05 Python
Python3.4 tkinter,PIL图片转换
Jun 21 Python
在python中实现对list求和及求积
Nov 14 Python
从运行效率与开发效率比较Python和C++
Dec 14 Python
Python字符串的常见操作实例小结
Apr 08 Python
Python socket 套接字实现通信详解
Aug 27 Python
解决安装新版PyQt5、PyQT5-tool后打不开并Designer.exe提示no Qt platform plugin的问题
Apr 24 Python
浅谈Python数学建模之整数规划
Jun 23 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
也谈 PHP 和 MYSQL
2006/10/09 PHP
phpmyadmin安装时提示:Warning: require_once(./libraries/common.inc.php)错误解决办法
2011/08/18 PHP
解析PHP无限级分类方法及代码
2013/06/21 PHP
深入php内核之php in array
2015/11/10 PHP
php删除一个路径下的所有文件夹和文件的方法
2018/02/07 PHP
javascript 异常处理使用总结
2009/06/21 Javascript
jquery EasyUI的formatter格式化函数代码
2011/01/12 Javascript
深入理解JavaScript系列(9) 根本没有“JSON对象”这回事!
2012/01/15 Javascript
js表单中选择框值的获取及表单的序列化
2015/12/17 Javascript
原生JS实现图片左右轮播
2016/12/30 Javascript
学好js,这些js函数概念一定要知道【推荐】
2017/01/19 Javascript
Javascript实现信息滚动效果
2017/05/18 Javascript
[49:35]2018DOTA2亚洲邀请赛3月30日 小组赛A组 KG VS TNC
2018/03/31 DOTA
使用go和python递归删除.ds store文件的方法
2014/01/22 Python
python实现数独算法实例
2015/06/09 Python
Python读取properties配置文件操作示例
2018/03/29 Python
Pandas 合并多个Dataframe(merge,concat)的方法
2018/06/08 Python
python使用pdfminer解析pdf文件的方法示例
2018/12/20 Python
python射线法判断检测点是否位于区域外接矩形内
2019/06/28 Python
python提取xml里面的链接源码详解
2019/10/15 Python
Python接口测试数据库封装实现原理
2020/05/09 Python
python数据类型强制转换实例详解
2020/06/22 Python
Django模型验证器介绍与源码分析
2020/09/08 Python
如何查看python关键字
2021/01/17 Python
VisionPros美国站:加拿大在线隐形眼镜和眼镜零售商
2020/02/11 全球购物
区域销售经理职责
2013/12/22 职场文书
服装店营销方案
2014/03/10 职场文书
班主任新年寄语
2014/04/04 职场文书
员工年终自我评价
2014/09/14 职场文书
党员教师学习党的群众路线教育实践活动心得体会
2014/10/31 职场文书
2014年班组建设工作总结
2014/12/01 职场文书
2014年幼儿园小班工作总结
2014/12/04 职场文书
英文感谢信格式
2015/01/21 职场文书
音乐课外活动总结
2015/05/09 职场文书
高中数学教学反思范文
2016/02/18 职场文书
银行求职信范文
2019/05/13 职场文书