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下尝试多线程编程
Apr 28 Python
python黑魔法之编码转换
Jan 25 Python
详解设计模式中的工厂方法模式在Python程序中的运用
Mar 02 Python
Python基于lxml模块解析html获取页面内所有叶子节点xpath路径功能示例
May 16 Python
对python中大文件的导入与导出方法详解
Dec 28 Python
Python数据报表之Excel操作模块用法分析
Mar 11 Python
python实现五子棋游戏
Jun 18 Python
django 2.2和mysql使用的常见问题
Jul 18 Python
python实现俄罗斯方块游戏(改进版)
Mar 13 Python
Python eval函数原理及用法解析
Nov 14 Python
python flask框架快速入门
May 14 Python
python数字图像处理实现图像的形变与缩放
Jun 28 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
浅析Dos下运行php.exe,出现没有找到php_mbstring.dll 错误的解决方法
2013/06/29 PHP
js 获取中文拼音,Select自动匹配字母获取值的代码
2009/09/23 Javascript
JQuery 返回布尔值Is()条件判断方法代码
2012/05/14 Javascript
js动画(animate)简单引擎代码示例
2012/12/04 Javascript
ExtJS中文乱码之GBK格式编码解决方案及代码
2013/01/20 Javascript
javascript数组操作(创建、元素删除、数组的拷贝)
2014/04/07 Javascript
javascript教程:关于if简写语句优化的方法
2014/05/17 Javascript
Js实现简单的小球运动特效
2016/02/18 Javascript
JavaScript比较当前时间是否在指定时间段内的方法
2016/08/02 Javascript
jQuery EasyUI封装简化操作
2016/09/18 Javascript
完美的js图片轮换效果
2017/02/05 Javascript
JS库particles.js创建超炫背景粒子插件(附源码下载)
2017/09/13 Javascript
AngularJS实现的获取焦点及失去焦点时的表单验证功能示例
2017/10/25 Javascript
关于vue编译版本引入的问题的解决
2018/09/17 Javascript
面试题:react和vue的区别分析
2019/04/08 Javascript
javascript数据类型中的一些小知识点(推荐)
2019/04/18 Javascript
vue不操作dom实现图片轮播的示例代码
2019/12/18 Javascript
vue 使用微信jssdk,调用微信相册上传图片功能
2020/11/13 Javascript
vue打开新窗口并实现传参的图文实例
2021/03/04 Vue.js
[46:38]完美世界DOTA2联赛PWL S2 Magma vs PXG 第三场 11.28
2020/12/02 DOTA
python使用循环实现批量创建文件夹示例
2014/03/25 Python
Python异常处理总结
2014/08/15 Python
在Django中创建动态视图的教程
2015/07/15 Python
学习python 之编写简单乘法运算题
2016/02/27 Python
Django实现简单分页功能的方法详解
2017/12/05 Python
使用Eclipse如何开发python脚本
2018/04/11 Python
浅析Python 读取图像文件的性能对比
2019/03/07 Python
python交互模式下输入换行/输入多行命令的方法
2019/07/02 Python
python中安装django模块的方法
2020/03/12 Python
Python中的With语句的使用及原理
2020/07/29 Python
HTML5中5个简单实用的API(第二篇,含全屏、可见性、拍照、预加载、电池状态)
2014/05/07 HTML / CSS
用C语言实现文件读写操作
2013/10/27 面试题
设计大赛策划方案
2014/06/13 职场文书
后勤管理员岗位职责
2014/08/27 职场文书
校园广播稿精选
2014/10/01 职场文书
鉴史问廉观后感
2015/06/10 职场文书