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使用xauth方式登录饭否网然后发消息
Apr 11 Python
解析Python中的生成器及其与迭代器的差异
Jun 20 Python
基于python的字节编译详解
Sep 20 Python
Python Numpy 数组的初始化和基本操作
Mar 13 Python
对python 生成拼接xml报文的示例详解
Dec 28 Python
Python写一个基于MD5的文件监听程序
Mar 11 Python
基于python-opencv3的图像显示和保存操作
Jun 27 Python
使用Python的networkx绘制精美网络图教程
Nov 21 Python
Django choices下拉列表绑定实例
Mar 13 Python
jupyter notebook 参数传递给shell命令行实例
Apr 10 Python
Python 常用日期处理 -- calendar 与 dateutil 模块的使用
Sep 02 Python
详解python tkinter包获取本地绝对路径(以获取图片并展示)
Sep 04 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
十大感人催泪爱情动漫 第一名至今不忍在看第二遍
2020/03/04 日漫
自定义php类(查找/修改)xml文档
2013/03/26 PHP
PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
2014/07/23 PHP
Yii框架关联查询with用法分析
2014/12/02 PHP
在Mac OS上搭建Nginx+PHP+MySQL开发环境的教程
2015/12/21 PHP
WordPress中登陆后关闭登陆页面及设置用户不可见栏目
2015/12/31 PHP
PHP后门隐藏的一些技巧总结
2020/11/04 PHP
浅析JS中document对象的一些重要属性
2014/03/06 Javascript
高性能JavaScript 重排与重绘(2)
2015/08/11 Javascript
js前端实现图片懒加载(lazyload)的两种方式
2017/04/24 Javascript
解决angular2在双向数据绑定时[(ngModel)]无法使用的问题
2018/09/13 Javascript
JavaScript组合设计模式--改进引入案例分析
2020/05/23 Javascript
Antd下拉选择,自动匹配功能的实现
2020/10/24 Javascript
[02:46]2014DOTA2国际邀请赛 选手为你解读比赛MVP充满梦想
2014/07/09 DOTA
[00:36]DOTA2勇士令状莱恩声望物品——冥晶之厄展示
2018/05/25 DOTA
[02:16]完美世界DOTA2联赛PWL S3 集锦第三期
2020/12/21 DOTA
Python中文编码那些事
2014/06/25 Python
python通过ssh-powershell监控windows的方法
2015/06/02 Python
浅谈用Python实现一个大数据搜索引擎
2017/11/28 Python
JS设计模式之责任链模式实例详解
2018/02/03 Python
python的pandas工具包,保存.csv文件时不要表头的实例
2018/06/14 Python
Django-migrate报错问题解决方案
2020/04/21 Python
python3:excel操作之读取数据并返回字典 + 写入的案例
2020/09/01 Python
详解HTML5中的Communication API基本使用方法
2016/01/29 HTML / CSS
美国女孩洋娃娃店:American Girl
2017/10/24 全球购物
加拿大折扣、优惠券和交易网站:WagJag
2018/02/07 全球购物
Paul’s Boutique官网:英国时尚手袋品牌
2018/03/31 全球购物
师范毕业生个人求职信
2013/12/09 职场文书
高校教师思想汇报
2014/01/11 职场文书
白酒市场营销方案
2014/02/25 职场文书
会计专业求职信范文
2014/03/16 职场文书
欢度春节标语
2014/07/01 职场文书
庆元旦活动总结
2014/07/09 职场文书
2016寒假假期总结
2015/10/10 职场文书
严以用权专题学习研讨会发言材料
2015/11/09 职场文书
SQL写法--行行比较
2021/08/23 SQL Server