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 相关文章推荐
在Linux系统上安装Python的Scrapy框架的教程
Jun 11 Python
python装饰器初探(推荐)
Jul 21 Python
python万年历实现代码 含运行结果
May 20 Python
Python 实现使用dict 创建二维数据、DataFrame
Apr 13 Python
基于python的多进程共享变量正确打开方式
Apr 28 Python
python实现本地图片转存并重命名的示例代码
Oct 27 Python
python爬虫学习笔记之Beautifulsoup模块用法详解
Apr 09 Python
一文轻松掌握python语言命名规范规则
Jun 18 Python
python 实现"神经衰弱"翻牌游戏
Nov 09 Python
Pycharm安装python库的方法
Nov 24 Python
pycharm远程连接服务器并配置python interpreter的方法
Dec 23 Python
Django实现翻页的示例代码
May 24 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 团购折扣计算公式
2011/11/24 PHP
组合算法的PHP解答方法
2012/02/04 PHP
Drupal7连接多个数据库及常见问题解决
2014/03/02 PHP
php+mysql实现用户注册登陆的方法
2015/01/03 PHP
PHP获取文件相对路径的方法
2015/02/26 PHP
针对thinkPHP5框架存储过程bug重写的存储过程扩展类完整实例
2018/06/16 PHP
PHP htmlentities()函数用法讲解
2019/02/25 PHP
laravel5.6中的外键约束示例
2019/10/23 PHP
Firefox+FireBug使JQuery的学习更加轻松愉快
2010/01/01 Javascript
基于jsTree的无限级树JSON数据的转换代码
2010/07/27 Javascript
javascript学习笔记(二十) 获得和设置元素的特性(属性)
2012/06/20 Javascript
JQuery select控件的相关操作实现代码
2012/09/14 Javascript
jQuery中attr()和prop()在修改checked属性时的区别
2014/07/18 Javascript
JavaScript运行机制之事件循环(Event Loop)详解
2014/10/10 Javascript
javascript实现根据iphone屏幕方向调用不同样式表的方法
2015/07/13 Javascript
AngularJS 遇到的小坑与技巧小结
2016/06/07 Javascript
JQueryEasyUI之DataGrid数据显示
2016/11/23 Javascript
vue-router2.0 组件之间传参及获取动态参数的方法
2017/11/10 Javascript
一些你可能不熟悉的JS知识点总结
2019/03/15 Javascript
10个最受欢迎的 JavaScript框架(推荐)
2019/04/24 Javascript
vue回到顶部监听滚动事件详解
2019/08/02 Javascript
Vue Object 的变化侦测实现代码
2020/04/15 Javascript
使用Python脚本将绝对url替换为相对url的教程
2015/04/24 Python
Python 探针的实现原理
2016/04/23 Python
Python用 KNN 进行验证码识别的实现方法
2018/02/06 Python
一看就懂得Python的math模块
2018/10/21 Python
Python爬虫实战之12306抢票开源
2019/01/24 Python
Python按钮的响应事件详解
2019/03/04 Python
Python实现二叉搜索树BST的方法示例
2019/07/30 Python
初中校园广播稿
2014/02/02 职场文书
《灰椋鸟》教学反思
2014/04/27 职场文书
食品安全演讲稿
2014/09/01 职场文书
学习朴航瑛老师爱岗敬业先进事迹思想汇报
2014/09/17 职场文书
大学生就业意向书
2015/05/11 职场文书
详解Vue的sync修饰符
2021/05/15 Vue.js
教你如何用Python实现人脸识别(含源代码)
2021/06/23 Python