码迷,mamicode.com
首页 > 编程语言 > 详细

python基础之文件操作

时间:2016-05-14 21:21:12      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

文件操作有很多种

  我们在这里可以大体分一下,文件的操作其实可以分为对文件整体的操作(创建文件,删除文件,重命名文件,获取文件属性)以及对文件内容的操作(修改文件内容)

  先来看对文件整体的操作:

我们按照增删查改的顺序进行讲述

1  增

  所谓增,即新建。

新建一个文件

f=open(‘lalal‘,‘w+‘)    #如果该文件(lalal)不存在则创建,若存在则打开。

新建一个目录:

>>> os.listdir()
[‘log‘, ‘test1‘]
>>> os.mkdir(‘bin‘)
>>> os.listdir()
[‘log‘, ‘bin‘, ‘test1‘]

 

2  删

  所谓删,也就是删除。

删除文件:

>>> import os
>>> os.listdir()
[‘log‘, ‘test‘, ‘bin‘, ‘test1‘]
>>> os.remove(‘test‘)     #仅仅只能删除文件,如果删除目录的话会报错。
>>> os.listdir()
[‘log‘, ‘bin‘, ‘test1‘]

删除目录:

>>> os.rmdir(‘bin‘)
>>> os.listdir()
[‘log‘, ‘test1‘]

3  查

  查看文件的各种属性,如:目录下包含的文件,创建时间  修改时间  访问时间  文件权限  文件大小……

查看目录下包含的文件以及目录:

>>> os.listdir()
[‘log‘, ‘bin‘, ‘test1‘]

获取文件/目录信息:

>>> os.stat(‘test1‘)
os.stat_result(st_mode=33188, st_ino=137156, st_dev=2050, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1463225144, st_mtime=1463225144, st_ctime=1463225144)

4  改

修改文件/目录的权限:

技术分享
>>> os.stat(‘test1‘)
os.stat_result(st_mode=33188, st_ino=137156, st_dev=2050, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1463225144, st_mtime=1463225144, st_ctime=1463225144)
>>> os.chmod(‘test1‘,3)           
>>> os.stat(‘test1‘)
os.stat_result(st_mode=32771, st_ino=137156, st_dev=2050, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1463225144, st_mtime=1463225144, st_ctime=1463225965)



[root@slyoyo python_test]# lsa
total 16
drwxr-xr-x. 4 root root 4096 May 14 04:31 .
dr-xr-x---. 5 root root 4096 May 14 04:25 ..
drwxr-xr-x. 2 root root 4096 May 14 04:31 bin
drwxr-xr-x. 2 root root 4096 May 14 04:25 log
-rw-r--r--. 1 root root    0 May 14 04:25 test1
[root@slyoyo python_test]# lsa
total 16
drwxr-xr-x. 4 root root 4096 May 14 04:31 .
dr-xr-x---. 5 root root 4096 May 14 04:25 ..
drwxr-xr-x. 2 root root 4096 May 14 04:31 bin
drwxr-xr-x. 2 root root 4096 May 14 04:25 log
--------wx. 1 root root    0 May 14 04:25 test1
View Code

修改文件内容(读写文件)

  读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作 系统打开一个文件对象(通常称为文件描述符),然后,通过操作系统提供的接口从这个文件对象中读取数据(读文件),或者把数据写入这个文件对象(写文件)。

  所以说,要想读或者写一个文件首先就要打开这个文件。

如何打开?

[root@slyoyo python_test]# cat test1 
hello world

>>> f=open(‘test1‘,‘r‘)     #打开文件
#格式open(‘file‘,mode)
file:文件
mode:

  w     以写方式打开,
  a     以追加模式打开 (从 EOF 开始, 必要时创建新文件)
  r+     以读写模式打开
  w+     以读写模式打开 (参见 w )
  a+     以读写模式打开 (参见 a )
  rb     以二进制读模式打开
  wb     以二进制写模式打开 (参见 w )
  ab     以二进制追加模式打开 (参见 a )
  rb+    以二进制读写模式打开 (参见 r+ )
  wb+    以二进制读写模式打开 (参见 w+ )
  ab+    以二进制读写模式打开 (参见 a+ )

如何读文件

fp.read([size])                    #size为读取的长度,以byte为单位

fp.readline([size])              #读一行,如果定义了size,有可能返回的只是一行的一部分

fp.readlines([size])           #把文件每一行作为一个list的一个成员,并返回这个list。其实它的内部是通过循环调用readline()来实现的。如果提供size参               #数,size是表示读取内容的总长,也就是说可能只读到文件的一部分。

[root@slyoyo python_test]# cat test1 
hello world
my name is liming
i live in shijiazhuang
a city not far from beijing

>>> f=open(‘test1‘,‘r‘)
>>> f.read()
‘hello world\nmy name is liming\ni live in shijiazhuang\na city not far from beijing\n‘

>>> f=open(‘test1‘,‘r‘)
>>> f.readline()
‘hello world\n‘

>>> f=open(‘test1‘,‘r‘)
>>> f.readlines()
[‘hello world\n‘, ‘my name is liming\n‘, ‘i live in shijiazhuang\n‘, ‘a city not far from beijing\n‘]


如何写文件?

fp.write(str)                      #把str写到文件中,write()并不会在str后加上一个换行符

fp.writelines(seq)            #把seq的内容全部写到文件中(多行一次性写入)。这个函数也只是忠实地写入,不会在每行后面加上任何东西。

>>> f=open(‘test1‘,‘w+‘)
>>> f.read()
‘‘      #当以写入的方式打开一个文件时,文件的原内容就不存在了

>>> sentence=["Everbody will make mistakes\n","that‘s why they put erasers on the end of pencils.\n"]
>>> f.writelines(sentence)
>>> f.readlines()
[]        #即使写入了内容,新内容仍无法见。
>>> f.close()
>>> f=open(‘test1‘,‘r‘)
>>> f.read()
"Everbody will make mistakes\nthat‘s why they put erasers on the end of pencils.\n"

 

python基础之文件操作

标签:

原文地址:http://www.cnblogs.com/MnCu8261/p/5493184.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!