Python基础(六) 基础文件操作

     今天学习python下对文件的基础操作,主要从open函数、File对象的属性、文件定位、简单操作、举例说明几个步骤开始学习,下面开始进入今天的主题:

一、open函数介绍

      open函数主要是打开一个文件,创建一个file对象,相关的方法可以调用它进行读写 。

语法格式如下:

1
2
3
file object = open(文件名,打开文件的模式)
 
file object  = with open (文件名,打开文件的模式) as 变量名

   两种语法格式的不同在于下面这种方法不用输入f.close()函数来关闭文件。

在介绍文件打开模式之前介绍两个python的函数:

   计算机中一个字节是8位,那么在不同的字符编码占用的字节不同,在计算机中为了节省内存空间,汉字一般是用16进制表示,

        UTF-8:一个汉字占计算机的三个字节

        GBK:一个汉字占两个字节

bytes():将字符串转换成字节类型

1
2
3
4
5
6
7
8
n = bytes("你好", encoding="utf-8")      #将字符串转换成utf-8的字节码
print(n)
n = bytes("你好", encoding="gbk")        #将字符串转换成gbk的字节码
print(n)
 
结果:
b'\xe4\xbd\xa0\xe5\xa5\xbd'
b'\xc4\xe3\xba\xc3'

str():可以将字节类型转换回字符串

1
2
3
4
5
new_str = str(bytes("你好", encoding="utf-8"), encoding="utf-8")
print(new_str)
 
结果:
你好

下面来看一下打开文件的模式:

二、File对象的属性和方法

 

      下面是file对象相关的所有属性的列表,举例说明这些属性的用法:

1
2
3
4
5
6
7
8
f = open('user.txt','a')
print("文件名:",f.name)
print("文件是否已关闭:",f.closed)
f.close()
 
结果:
文件名: user.txt
文件是否已关闭: False

      下面介绍一下file对象的方法

  1,file.read()

1
2
3
4
5
6
f = open('user.txt','r')
print(f.read())                   # 从文件中读取出字符串
 
结果:
UserName='jack'
PassWord='123456'

  2,file.readline()

1
2
3
4
5
f = open('user.txt','r')
print(f.readline())               #读取一行
 
结果:
UserName='jack'

  3,file.readlines()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
f = open('user.txt','r')
print(f.readlines())       #把文件每一行作为一个list的一个成员,并返回这个list。
 
结果:
["UserName='jack'\n", "PassWord='123456'\n"]
 
f = open('user.txt','r')
for i in f.readlines():          #也可以通过循环调用readline()来实现的
    print(i)
 
结果:
UserName='jack'
 
PassWord='123456'

4,file.write()

      write()方法可将任何字符串写入一个打开的文件。需要重点注意的是,Python字符串可以是二进制数据,而不是仅仅是文字。注意:Write()方法不在字符串的结尾不添加换行符('\n'): 

1
2
3
4
5
6
7
8
f = open('user.txt','a')          #追加的模式打开文件
f.write("age:18\n")     
f.close()
 
结果:
UserName='jack'
PassWord='123456'
age:18

  5,file.writelines()

     用于向文件中写入一序列的字符串。这一序列字符串可以是由迭代对象产生的,如一个字符串列表;

     换行需要制定换行符 \n。

1
2
3
4
5
6
7
8
9
10
11
f = open('user.txt','a')
print("文件名:",f.name)
seq = ["age=19\n","sex=man\n"]
f.writelines(seq)
f.close()
 
结果:
UserName='jack'
PassWord='123456'
age=19
sex=man

  6,file.flush() 

       刷新文件内部缓冲,直接把内部缓冲区的数据立刻写入文件, 而不是被动的等待输出缓冲区写入。

1
2
3
4
f =open('user','r+')
f.write('123')
f.flush()                      #通过强制刷新,123已经写入到文件中
name = input('input:')         #在这会一直等待用户输入

  7,file.truncate( [size] )

      截取文件,截取的字节通过size指定,默认为当前文件位置。

 

1
2
3
4
f =open('user','r+',encoding="utf-8")
f.seek(3)                     #将指针移动到第3个位置
f.truncate()                  #指针以后清空
f.close()

三、文件定位、重命名和删除文件

      (一)、文件定位

        file.tell() : 方法告诉你文件内的当前位置;换句话说,下一次的读写会发生在文件开头这么多字节之后。

        file.seek(offset [,from]): 方法改变当前文件的位置。Offset变量表示要移动的字节数。From变量指定开始移动字节的参考位置。

        如果from被设为0,这意味着将文件的开头作为移动字节的参考位置。如果设为1,则使用当前的位置作为参考位置。如果它被设为2,那么该文件的末尾将作为参考位置。 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
f = open("user.txt",'r+')               #读写的模式打开文件
str = f.read(10)
print("读取的字符串是:",str)
  
position = f.tell()                     #找到当前指针位置
print("指针位置:",position)
  
position = f.seek(5,0)                  #将指针移动到从开头往后移动5个字节
str=f.read(10)
print(str)
f.close()
 
结果:
读取的字符串是: weibo.com.
指针位置: 10
.com.cn

      (二)、重命名和删除文件

 Python的os模块提供了执行文件处理操作的方法,比如重命名和删除文件。

        rename()方法:rename()方法需要两个参数,当前的文件名和新文件名。 

1
2
import os
os.rename("user.txt","test.txt")

        remove()方法:删除文件,需要提供要删除的文件名作为参数。

1
2
import os
 os.remove("test.txt")            #删除已经存在的文件

     (三)、知识点拾遗

    在python2.7以后with open() 可以同时打开两个文件,具体操作如下:

1
2
3
4
5
6
with open('user','r',encoding='utf-8') as f1,open('user1','w',encoding='utf-8')as f2:
    for line in f1:                                 #将f1一行一行的读取出来
        new_str=line.replace('haifeng','idx')       #替换其中的某个字符串
        f2.write(new_str)#                          #将替换好的文件写到新文件里
os.rename('user','user.bak')                        #备份旧文件
os.remove('user1','user')                           #发布新文件
1
2
3
4
5
6
7
8
9
10
11
12
f =open('user','r+')                                #以读写的方式打开文件
Name=input("input:")                                #获取用户输入
for i in f.readlines():                             #已列表的方式一行一行读取文件
    line = i.strip()                                #去除空格,空行
    last_line = line.split("=")                     #按=号分割成新列表
    if last_line[0] == 'name':                      #判断如果是name的参数就行替换
        last_line[1] = str(Name)
        new_str = '='.join(last_line)               #将新列表转换成字符串
f.seek(0,0)                                         #将指针调到起始位置开始
f.write(new_str)                                    #写入写字符串
f.truncate()                                        #将多余的字符删除
f.close()                                           #关闭文件

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def fetch(args):                           #定义查询的函数
    result=[]
    with open('haproxy.conf','r') as f:    #以只读的方式打开文件
        flag = False                       #定义跳出符
        for line in f:
            #如果匹配到第一个开头是backend并且内容等于backend整条记录,跳出循环进行下一次循环
            if line.strip().startswith('backend') and line.strip() == 'backend {0}'.format(args):
                flag = True
                continue
            #如果flag=True并且匹配到第二个backend时候,break,取第一个backend到第二个backend中间的值
            if flag and line.strip().startswith('backend'):
                flag = False
                break
            #如果flag=True并且行的内容为空行的时候,将新纪录添加到列表里,return 列表的值
            if flag and line.strip():
                result.append(line.strip())
    return result
 
def add(backend, record):
    record_list = fetch(backend)  
    #调用上面的查询函数,判断列表是否为None(空),即backend和record都不存在
    if not record_list:
    #边写边读,在文件末尾添加新的纪录和backend
        with open('ha.conf', 'r') as old, open("new.conf", 'w') as new:
            for line in old:
                new.write(line)
            new.write("\nbackend " + backend + "\n")
            new.write(" " * 8 + record + "\n")
    else:
        if record in record_list:
            # record已经存在
             pass
        else:
            # backend存在,record不存在
            record_list.append(record)     #先将新纪录添加到列表里
            with open('haproxy.conf', 'r') as old, open('new.conf', 'w') as new:
                flag = False
                for line in old:
                    if line.strip().startswith("backend") and line.strip() == "backend " + backend:
                        flag = True
                        new.write(line)
                        for new_line in record_list:
                            new.write(" " * 8 + new_line + "\n")
                    #读取到第一个backend时,将backend那一行写入新文件,然后循环记录的列表,将新纪录的列表
                    #循环写进新文件,知道匹配到写一个backend为止
                        continue
                    if flag and line.strip().startswith("backend"):
                        flag = False
                        new.write(line)
                        continue
                    if line.strip() and not flag:
                        new.write(line)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def add2(backend,record):
    with open('haproxy.conf','r')as old,open('new.conf','w')as new:
        in_backend = False
        has_backend =False
        has_record = False
        for line in old:
            if line.strip().startswith('backend') and line.strip() == "backend "+backend:
                has_backend=True
                in_backend=True
                new.write(line)
                continue
            if in_backend and line.strip().startswith('backend'):
                if not has_record:
                    new.write(" "*8 + record +'\n')
                new.write(line)
                in_backend = False
                continue
            if in_backend and line.strip() ==record:
                has_record =True
                new.write(line)
                continue
            if line.strip():
                new.write(line)
        if not  has_backend:
            new.write('backend '+backend+'\n')
            new.write(' '*8 +record +'\n')
bk = "www.oldboy.org"
rd = "server 100.1.7.49 100.1.7.49 weight 20 maxconn 30"
add2(bk, rd)

推荐使用第一种方法进行文件内容的更新,基础的文件操作就介绍到这里。





posted @ 2016-05-25 23:36  邸海峰  阅读(275)  评论(0编辑  收藏  举报
doc