搜尋
首頁後端開發Python教學拿下 Python中的檔案操作

拿下 Python中的檔案操作

Nov 09, 2020 pm 05:36 PM
python文件操作

Python影片教學欄位介紹相關文件操作。

拿下 Python中的檔案操作

任何語言都離不開檔案的操作,那麼Python語言是如何來操作和管理檔案的。

編碼方式

編碼方式的歷史大致為ASCII ->gb2312->unicode#-> utf-8,期間具體詳細資訊可以百度

來個編碼解碼的小例子先,記住中文可以進行GBKutf-8 編碼,在GBk一個中文字元對應兩個字節,在utf-8一個中文字元對應三個字節,中文不能進行ASCII編碼。

>>> '刘润森'.encode('GBK')
b'\xc1\xf5\xc8\xf3\xc9\xad'
>>> '刘润森'.encode('ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
>>> 'Runsen'.encode('ascii')
b'Runsen'
>>> "刘润森".encode('utf-8')
b'\xe5\x88\x98\xe6\xb6\xa6\xe6\xa3\xae'
>>> '刘润森'.encode('GBK').decode('GBK')
'刘润森'
>>> '刘润森'.encode('GBK').decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc1 in position 0: invalid start byte复制代码</module></stdin>

如果編碼解碼格式不一致可能會出現亂碼,encode表示編碼,decode表示解碼。

檔案操作的API

以下是Python檔案操作的特定的API。

##讀取目前指標位置
方法 含義
open 開啟
read 讀取
write 寫入
#close 關閉
readline #單行讀取
readlines 多行讀取
seek 檔案指標操作
#tell
開啟檔案

Python的

open()函數開啟一個檔案時,有若干個參數可用。然而,最常用的參數只有前兩個。

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

注意,第一個是強制性的,其餘的是可選的。如果不加入mode參數,檔案將在Python中以唯讀模式開啟。

encoding:可以不寫。不寫參數,預設的編碼本是作業系統預設的編碼本。 windows預設gbk,linux預設utf-8,mac預設utf-8。

f=open('test.txt',encoding='utf-8')   #打开文件
data=f.read()  #读取文件
print(data)
f.close() #关闭文件         
复制代码
mode

mode#意思##r rbwwba
文字模式,讀取
二進位模式,讀取
文字模式,寫入
二元模式,寫入
#################################################################################### #文字模式,追加############ab#######二進位模式,追加############ #######可讀可寫############

读取文件

代码中用到的文件文件操作的1.txt 文件内容如下:

关注《Python之王》公众号
作者:Runsen复制代码

readline(),使用该方法时,需要指定打开文件的模式为r或者r+;

readlines(),读取全部行.返回一个列表,列表中的每个元素是原文件的每一行。如果文件很大,占内存,容易崩盘。

# 打开文件进行读取
f = open("1.txt","r",encoding='utf-8')
# 根据大小读取文件内容
print('输出来自 read() 方法\n',f.read(2048))
# 关闭文件
f.close()
# 打开文件进行读写
f = open("1.txt","r+",encoding='utf-8')
# 读取第2个字和第2行行的文件内容
print('输出来自 readline() 方法\n',f.readline(2))
print('输出来自 readlines() 方法\n',f.readlines(2))
# 关闭文件
f.close()
# 打开文件进行读取和附加
f = open("1.txt","r",encoding='utf-8')
# 打开文件进行读取和附加
print('输出来自 readlines() 方法\n',f.readlines())
# 关闭文件
f.close()

# 输出如下
输出来自 read() 方法
 关注《Python之王》公众号
作者:Runsen
输出来自 readline() 方法
 关注
输出来自 readlines() 方法
 ['《Python之王》公众号\n']
输出来自 readlines() 方法
 ['关注《Python之王》公众号\n', '作者:Runsen']复制代码

写入文件

下面只介绍清除写 w追加写 a

案例:将关注《Python之王》公众号写入 test.txt 文件中

# mode=w 没有文件就创建,有就清除内容,小心使用
with open('test.txt', 'w', encoding='utf-8') as fb:
      fb.write('关注《Python之王》公众号\n')  
复制代码

下面再将作者:Runsen写入test.txt 文件中

with open('test.txt', 'w', encoding='utf-8') as fb:
      fb.write('作者:Runsen\n')  
复制代码

运行后会发现之前写的关注《Python之王》公众号内容修改为作者:Runsen,因为 w模式会清除原文件内容,所以小心使用。只要使用了w,就要一次性写完。

追加写 a

案例:将静夜思这首诗追加到 test.txt 文件中

# mode=a 追加到文件的最后
with open('test.txt', 'a', encoding='utf-8') as fb:
      fb.write('关注《Python之王》公众号\n')  
with open('test.txt', 'a'encoding='utf-8') as fb:
      fb.write('作者:Runsen\n')      
复制代码

指针操作

事物或资源都是以文件的形式存在,比如消息、共享内存、连接等,句柄可以理解为指向这些文件的指针。

句柄(handle)是一个来自编译原理的术语,指的是一个句子中最先被规约的部分,所以带有一个「句」字。

句柄的作用就是定位,两个APi还是tell和seek。

tell返回文件对象在文件中的当前位置,seek将文件对象移动到指定的位置,传入的参数是offset ,表示移动的偏移量。

下面通过示例对上述函数作进一步了解,如下所示:

with open('test.txt', 'rb+') as f:
    f.write(b'Runsen')
    # 文件对象位置
    print(f.tell())
    # 移动到文件的第四个字节
    f.seek(3)
    # 读取一个字节,文件对象向后移动一位
    print(f.read(1))
    print(f.tell())
    # whence 为可选参数,值为 0 表示从文件开头起算(默认值)、值为 1 表示使用当前文件位置、值为 2 表示使用文件末尾作为参考点
    # 移动到倒数第二个字节
    f.seek(-2, 2)
    print(f.tell())
    print(f.read(1))
    
#输出如下
6
b's'
4
50
b'\r' 
复制代码

上下文管理

我们会进行这样的操作:打开文件,读写,关闭文件。程序员经常会忘记关闭文件。上下文管理器可以在不需要文件的时候,自动关闭文件,使用with open即可。

# with context manager
with open("new.txt", "w") as f:
    print(f.closed)
    f.write("Hello World!")
print(f.closed)

#输出如下
False
True复制代码

如何批量读取多个文件

下面,批量读取某文件夹下的txt文件

file_list = ['1.txt', '2.txt', '3.txt','4.txt']
for path in file_list:
    with open(path, encoding='utf-8') as f:
        for line in f:
            print(line)复制代码

下面将批量读取文件夹下的txt文件的内容,合并内容到一个新文件5.txt中,具体实现的代码如下。

import os
#获取目标文件夹的路径
filedir = os.getcwd()+'\\'+'\\txt'
#获取当前文件夹中的文件名称列表
filenames = []
for i in os.listdir(filedir):
    if i.split(".")[-1] == 'txt':
        filenames.append(i)
#打开当前目录下的5.txt文件,如果没有则创建
f = open('5.txt','w')
#先遍历文件名
for filename in filenames:
    filepath = filedir+'\\'+filename
    #遍历单个文件,读取行数
    for line in open(filepath,encoding='utf-8'):
        f.writelines(line)
        f.write('\n')
#关闭文件
f.close()复制代码

其实在Window中只需要cd 至目标文件夹,即你需要将所有想要合并的txt文件添加至目标文件夹中,执行如下DOS命令  type *.txt > C:\目标路径\合并后的文件名.txt

练习

题目:创建文件data.txt,文件共100000行,每行存放一个1~100之间的整数,题目来源:牛客

import random

f = open(‘data.txt’,‘w+’)
for i in range(100000):
  f.write(str(random.randint(1,100)) + ‘\n’)
  f.seek(0)
  print(f.read())
  f.close()复制代码

题目:生成100个MAC地址并写入文件中,MAC地址前6位(16进制)为01-AF-3B,题目来源:牛客

import random
import string

def create_mac():
  MAC='01-AF-3B'
  hex_num =string.hexdigits #0123456789abcdefABCDEF
  for i in range(3):
    n = random.sample(hex_num,2)
    sn = '-' + ''.join(n).upper()
    MAC += sn
  return MAC

def main():
  with open('mac.txt','w') as f:
    for i in range(100):
      mac = create_mac()
      print(mac)
      f.write(mac+'\n')

main()复制代码

相关免费学习推荐:python视频教程

以上是拿下 Python中的檔案操作的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:juejin。如有侵權,請聯絡admin@php.cn刪除
列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

說明如何將內存分配給Python中的列表與數組。說明如何將內存分配給Python中的列表與數組。May 03, 2025 am 12:10 AM

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

您如何在Python數組中指定元素的數據類型?您如何在Python數組中指定元素的數據類型?May 03, 2025 am 12:06 AM

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

什麼是Numpy,為什麼對於Python中的數值計算很重要?什麼是Numpy,為什麼對於Python中的數值計算很重要?May 03, 2025 am 12:03 AM

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

討論'連續內存分配”的概念及其對數組的重要性。討論'連續內存分配”的概念及其對數組的重要性。May 03, 2025 am 12:01 AM

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

在Numpy陣列上可以執行哪些常見操作?在Numpy陣列上可以執行哪些常見操作?May 02, 2025 am 12:09 AM

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Python的數據分析中如何使用陣列?Python的數據分析中如何使用陣列?May 02, 2025 am 12:09 AM

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器