Python的檔案操作

小周啊發表於2019-05-13

1、 讀寫檔案

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2018/1/25 20:49
# @Author  : zhouyuyao
# @File    : demonWrite.py
# PyCharm 2017.3.2 (Community Edition)
# Build #PC-173.4127.16, built on December 19, 2017
# JRE: 1.8.0_152-release-1024-b8 amd64
# JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
# Windows 10 10.0
# Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) 
# [MSC v.1900 64 bit (AMD64)] on win32

if __name__== "__main__":
    filename = input("Please input the name of file:")
    f = open(filename,"w")     # 以寫的形式開啟一個檔案
    while 1:         # 1 的效率是最高的
        context = input("Please input context(`EOF` will close file): ")
        if context == "EOF":
            f.close()
            break
        else:
            f.write(context)
            f.write("
")

    fRead = open(filename)
    readContext = fRead.read()
    print("------------start-------------")
    print(readContext)
    print("-------------end--------------")
    fRead.close()

執行結果:

Please input the name of file:z.log
Please input context(`EOF` will close file): hello
Please input context(`EOF` will close file): the weather is cool
Please input context(`EOF` will close file): you have wear more clothes
Please input context(`EOF` will close file): EOF
------------start-------------
hello
the weather is cool
you have wear more clothes

-------------end--------------

2、 讀取檔案方法

import codecs

ENCODING = "utf-8"       # 字符集
f = open("z.log",encoding=ENCODING)
print(f.name)            # 檔名
print(f.readline())      # 讀取成列表的形式
print(f.readlines())     # 讀取成列表的形式

with codecs.open("z.log","r",encoding=ENCODING) as f:
    print(f.read())

3、 編碼問題

編碼:
支援中文的編碼:utf-8,gbk,gb2312

decode 解碼
encode 編碼

在Python2中不定義程式碼的編碼排頭,在內容中出現中文時會報錯。
Python預設將程式碼檔案內容當做ASCII編碼處理,但是ASCII編碼不存在中文,因為則會丟擲異常。
解決問題之道就是要讓Python之道檔案中使用的是什麼編碼形式,對於中文,可以用的常見編碼有utf-8,gbk和gb2312等,只需在程式碼檔案的最前端新增如下內容即可:

# -*- coding:utf-8 -*-

Python轉碼的過程:
原有編碼 ——> Unicode編碼 ——> 目的編碼

python會自動將帶中文的字串解碼成Unicode,然後再編碼成gbk,因為解碼是字典進行的,如果沒有指明解碼方式,就會使用sys,defaultencoding指明的方式來解碼。
方法一:
s.decode("utf-8").encoding("gbk")

4、對檔案進行排序

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2018/1/25 23:06
# @Author  : zhouyuyao
# @File    : sortUIDPasswd.py
# PyCharm 2017.3.2 (Community Edition)
# Build #PC-173.4127.16, built on December 19, 2017
# JRE: 1.8.0_152-release-1024-b8 amd64
# JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
# Windows 10 10.0
# Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) 
# [MSC v.1900 64 bit (AMD64)] on win32
import codecs

file = "passwd"
sortfile = "sortpasswd.txt"
filecontext = []
sortuid = []

with codecs.open(sortfile,"wb") as fsort:
    with codecs.open(file,encoding="utf-8") as f:
        filecontext += f.readlines()
        for line in filecontext:
            sortuid.append(int(line.split(":")[2]))
        sortuid.sort()
        for uid in sortuid:
            for line in filecontext:
                if str(uid) == line.split(":")[2]:
                    print(line)
                    fsort.write(line.encode("utf-8"))

python3的新特性對文字和二進位制資料作了更為清晰的區分,
文字總是Unicode,由str型別表示,
二進位制則是由bytes型別表示
字串可以encode編碼成位元組包,而位元組包可以decode解碼成字串


相關文章