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中sys.argv参数用法实例分析
May 20 Python
详解Python编程中time模块的使用
Nov 20 Python
Python中判断输入是否为数字的实现代码
May 26 Python
python的格式化输出(format,%)实例详解
Jun 01 Python
python3实现SMTP发送邮件详细教程
Jun 19 Python
python处理multipart/form-data的请求方法
Dec 26 Python
django框架用户权限中的session缓存到redis中的方法
Aug 06 Python
Django Model中字段(field)的各种选项说明
May 19 Python
Numpy实现卷积神经网络(CNN)的示例
Oct 09 Python
python多线程和多进程关系详解
Dec 14 Python
Python将QQ聊天记录生成词云的示例代码
Feb 10 Python
python正则表达式re.search()的基本使用教程
May 21 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查询域名状态whois的类
2006/11/25 PHP
DISCUZ在win2003环境下 Unable to access ./include/common.inc.php in... 的问题终极解决方案
2011/11/21 PHP
php检查函数必传参数是否存在的实例详解
2017/08/28 PHP
实例分析基于PHP微信网页获取用户信息
2017/11/24 PHP
PHP实现的函数重载功能示例
2018/08/03 PHP
PHP基于openssl实现非对称加密代码实例
2020/06/19 PHP
PHP代码覆盖率统计详解
2020/07/22 PHP
jQuery 注意事项 与原因分析
2009/04/24 Javascript
利用WebBrowser彻底解决Web打印问题(包括后台打印)
2009/06/22 Javascript
js判断样式className同时增加class或删除class
2013/01/30 Javascript
输入自动提示搜索提示功能的使用说明:sugggestion.txt
2013/09/02 Javascript
Javascript中引用示例介绍
2014/02/21 Javascript
jQuery.datatables.js插件用法及api实例详解
2016/10/28 Javascript
js动态添加表格逐行添加、删除、遍历取值的实例代码
2018/01/25 Javascript
vue2.0 子组件改变props值,并向父组件传值的方法
2018/03/01 Javascript
Vue项目中使用jquery的简单方法
2019/05/16 jQuery
layui 监听select选择 获取当前select的ID名称方法
2019/09/24 Javascript
layui使用及简单的三级联动实现教程
2020/12/01 Javascript
[02:54]DOTA2英雄基础教程 撼地者
2014/01/14 DOTA
Python中的MongoDB基本操作:连接、查询实例
2015/02/13 Python
Pandas中把dataframe转成array的方法
2018/04/13 Python
python同步windows和linux文件
2019/08/29 Python
Jupyter notebook如何修改平台字体
2020/05/13 Python
python网络编程之五子棋游戏
2020/05/14 Python
python实现自动清理重复文件
2020/08/24 Python
python利用xlsxwriter模块 操作 Excel
2020/10/14 Python
详解css3自定义滚动条样式写法
2017/12/25 HTML / CSS
html如何对span设置宽度
2019/10/30 HTML / CSS
英国绿色商店:Natural Collection
2019/05/03 全球购物
Myprotein西班牙官网:欧洲第一大运动营养品牌
2020/02/24 全球购物
物流管理专业毕业生自荐信
2014/03/04 职场文书
运动会5000米加油稿
2015/07/21 职场文书
2015年社区消防安全工作总结
2015/10/14 职场文书
会计工作自我鉴定范文
2019/06/21 职场文书
javascript条件式访问属性和箭头函数介绍
2021/11/17 Javascript
解决 redis 无法远程连接
2022/05/15 Redis