`
alanland
  • 浏览: 634291 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Python 文件操作 和 目录操作

阅读更多

我们知道,文件名、目录名和链接名都是用一个字符串作为其标识符的,但是给我们一个标识符,我们该如何确定它所指的到底是常规文件文件名、目录名还是链接名呢?这时,我们可以使用os.path模块提供的isfile函数、isdir函数和islink函数来达成我们的目标,如下所示:
  print myfile, ’是一个’,

  if os.path.isfile(myfile):

  print ’plain file’

  if os.path.isdir(myfile):

  print ’directory’

  if os.path.islink(myfile):

  print ’link’
  您还可以查找文件的日期及其大小:
  time_of_last_access = os.path.getatime(myfile)

  time_of_last_modification = os.path.getmtime(myfile)

  size = os.path.getsize(myfile)
  这里的时间以秒为单位,并且从1970年1月1日开始算起。为了获取以天为单位的最后访问日期,可以使用下列代码:
  import time # time.time()返回当前时间

  age_in_days = (time.time()-time_of_last_access)/(60*60*24)
  为了获取文件的详细信息,可以使用os.stat函数和stat模块中的其它实用程序来达到目的,如下:
  import stat

  myfile_stat = os.stat(myfile)

  size = myfile_stat[stat.ST_SIZE]

  mode = myfile_stat[stat.ST_MODE]

  if stat.S_ISREG(mode):

  print ’%(myfile)是一个常规文件,大小为 %(size)d 字节’ %\

  vars()
  有关stat模块的详细信息,请参见Python Library Reference。若想测试一个文件的读、写以及执行权限,可以用os.access函数,具体如下所示:
  if os.access(myfile, os.W_OK):

  print myfile, ’具有写权限’

  if os.access(myfile, os.R_OK | os.W_OK | os.X_OK):

  print myfile, ’具有读、写以及执行权限’

 

 

 

http://tech.it168.com/a2009/0708/602/000000602694_1.shtml

 

 

       在很多应用中,文件操作是一个基本的功能,也是很重要的一部分.相对于其他语言来说,python对文件操作非常简单

       读和写:
       从文本中读取数据和把数据写进文本是文本的基本操作.这个非常简单.我们打开一个文本准备写数据:
       fp = open ( "test.txt", "w" )
       "w"表明我们将要把数据写到文本中.剩下的就比较容易理解.下一步实把数据写进文本中:
        fp.write ( ' This is a test. \nReady, it is. ' )
        这就把字符串"This is a test."写进文本的第一行,"Really , it is."写到了第二行.最后,我们需要清理和关闭文本.
       fp.close( )
         正如你所看到的,这个很容易,特别是对python的对象.但要清楚,当你使用"w"模式去再次写数据到文本中的时候,文本中的所有内容都会背删除掉.为了解决这个问题,可以使用"a"模式去把数据追加到文本末尾,添加数据到末尾:
         fp = open ( ' test.txt ', ' a ' )
         fp.write ( ' \n\n\nBottom line. ' )
         fp.close (  )
          现在我们把文本的数据读出来并显示:
         fp = open ( ' test.txt ' )
          print  fp.read ( )
         fp.close( )
          这把文本中数据全部读取出来并显示出来.我们也可以读取文本中的一行数据:
         fp = open ( ' test.txt ' )
          print fp.readline ( ) # " This is a test . "
          同样也可以把文本中的所有行存储到一个list中:
           fp = open ( ' test.txt ' )
           fileList = fp.readlines( )
           for fileline in fileList :
                 print '>>', fileline
           fp.close( )
           当在从文本中读取数据时,Python会记住指针在文本中的位置,如下例子:
           fp = open ( ' test.txt ' )
           garbage = fp.readline( )
           fp.readline ( ) # "Really, it is. "
           fp.close ( )
           只有第二行显示出来.当然,我们也可以通过把指针定位到其他地方来读取数据.
           fp = open ( 'test.txt' )
           garbage = fp.readline ( )
           fp .seek ( 0 )
           print  fp.readline ( ) # " This is a test . "
           fp.close ( )
           由上面的例子可以知道,我们可以告诉python继续从文本的第一个字节开始读取数据. 因此,第一行数据被打印出来. 我们同样可以要求python告诉指针当前位置:
           fp = open ( ' test.txt ' )
           print  fp.readlien ( )  # " This is a test "
           print  fp .tell ( 0 )  # " 17 "
           print fp.readline( )   # " Really , it is "    
          同样,一次也可以读取指定字节数的数据:
          fp  = open ( ' test.txt ' )
          print fp ( 1 ) # " T "
          fp.seek ( 4 )
          print fp.read ( 1 ) # " T "
          当我们在Windows 和 Macintosh平台时,有时候可以需要以二进制的模式来写数据,比如图片文件.为了做到这点,只要以"b"模式打开文本:
          fp = open ( ' testBinary.txt ', ' wb ' )
          fp.write ( ' There is no spoon. ' )
          fp.close ( )
          fp = open ( ' testBinary.txt ' , ' rb ' )
          print fp.read ( )
          fp .close ( )

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics