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递归打印某个目录的内容(实例讲解)
Aug 30 Python
Python实现发送与接收邮件的方法详解
Mar 28 Python
NumPy 数学函数及代数运算的实现代码
Jul 18 Python
解决Djang2.0.1中的reverse导入失败的问题
Aug 16 Python
python3 实现函数写文件路径的正确方法
Nov 27 Python
python实现ftp文件传输功能
Mar 20 Python
Python基于pyecharts实现关联图绘制
Mar 27 Python
学习python需要有编程基础吗
Jun 02 Python
如何在Python对Excel进行读取
Jun 04 Python
浅谈TensorFlow之稀疏张量表示
Jun 30 Python
Python 如何查找特定类型文件
Aug 17 Python
paramiko使用tail实时获取服务器的日志输出详解
Dec 06 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
8个出色的WordPress SEO插件收集
2011/02/26 PHP
php中stdClass的用法分析
2015/02/27 PHP
PHP爬虫之百万级别知乎用户数据爬取与分析
2016/01/22 PHP
PHP结合Ueditor并修改图片上传路径
2016/10/16 PHP
PHP实现的Redis多库选择功能单例类
2017/07/27 PHP
JavaScript 图片预览效果 推荐
2009/12/22 Javascript
javascript 伪数组实现方法
2010/10/11 Javascript
js获取图片大小的函数代码
2011/09/20 Javascript
Prototype源码浅析 Enumerable部分之each方法
2012/01/16 Javascript
JavaScript自定义方法实现trim()、Ltrim()、Rtrim()的功能
2013/11/03 Javascript
javascript使用定时函数实现跳转到某个页面
2013/12/25 Javascript
jQuery中extend函数详解
2015/02/13 Javascript
原生js实现倒计时功能(多种格式调用)
2017/01/12 Javascript
jQuery弹出窗口简单实现代码
2017/03/09 Javascript
在百度搜索结果中去除掉一些网站的资料(通过js控制不让显示)
2017/05/02 Javascript
利用ECharts.js画K线图的方法示例
2018/01/10 Javascript
[00:27]DOTA2战队VP、Secret贺新春
2018/02/11 DOTA
[44:37]完美世界DOTA2联赛PWL S3 Forest vs access 第一场 12.11
2020/12/13 DOTA
合并Excel工作薄中成绩表的VBA代码,非常适合教育一线的朋友
2009/04/09 Python
Python面向对象程序设计之继承与多继承用法分析
2018/07/13 Python
检测python爬虫时是否代理ip伪装成功的方法
2019/07/12 Python
python PIL和CV对 图片的读取,显示,裁剪,保存实现方法
2019/08/07 Python
python库matplotlib绘制坐标图
2019/10/18 Python
Python 多线程共享变量的实现示例
2020/04/17 Python
Sneaker Studio乌克兰:购买运动鞋
2018/03/26 全球购物
介绍一下gcc特性
2012/01/20 面试题
教师竞聘演讲稿
2014/05/16 职场文书
无刑事犯罪记录证明
2014/09/18 职场文书
解约证明模板
2015/06/19 职场文书
经典爱情感言
2015/08/03 职场文书
小学三年级数学教学反思
2016/02/16 职场文书
Python基础数据类型tuple元组的概念与用法
2021/08/02 Python
利用 JavaScript 构建命令行应用
2021/11/17 Javascript
Python3的进程和线程你了解吗
2022/03/16 Python
微信小程序 根据不同用户切换不同TabBar
2022/04/21 Javascript
PHP 时间处理类Carbon
2022/05/20 PHP