日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python读取文件内容操作_Python 3.6 读取并操作文件内容

發(fā)布時間:2025/3/12 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python读取文件内容操作_Python 3.6 读取并操作文件内容 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

下面為大家分享一篇Python 3.6 讀取并操作文件內(nèi)容的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起過來看看吧

所使用python環(huán)境為最新的3.6版本

Python中幾種對文件的操作方法:

將A文件復(fù)制到B文件中去(保持原來格式)

讀取文件中的內(nèi)容,返回List列表 (加載本地詞典庫)

讀取文件,返回文件內(nèi)容

#!/usr/bin/env python

# encoding: utf-8

"""

@author: wugang

@contact: 752820344@qq.com

@software: PyCharm

@file: toolkits_file.py

@time: 2017/3/1 0001 17:01

"""

'''

對文件操作的工具模塊

'''

# 1.將A文件復(fù)制到B文件中去(保持原來格式)

def copy_file (inputFile, outputFile, encoding):

fin = open(inputFile, 'r', encoding=encoding) #以讀的方式打開文件

fout = open(outputFile, 'w', encoding=encoding) #以寫得方式打開文件

for eachLiine in fin.readlines(): #讀取文件的每一行

line = eachLiine.strip() #去除每行的首位空格

fout.write(line + '\n')

fin.close()

fout.close()

# 2. 讀取文件中的內(nèi)容,返回List列表 (加載本地詞典庫)

def read_file_list(inputFile, encoding):

results = []

fin = open(inputFile, 'r', encoding=encoding)

for eachLiine in fin.readlines():

line = eachLiine.strip().replace('\ufeff', '')

results.append(line)

fin.close()

return results

# 3.讀取文件,返回文件內(nèi)容

def read_file(path):

with open(path, 'r+', encoding='UTF-8') as f:

str = f.read()

return str.strip().replace('\ufeff', '')

def func():

pass

if __name__ == '__main__':

copy_file('../data/test1.txt', '../data/text.txt','UTF-8')

contents = read_file_list('../dict/time.dict','UTF-8')

print(contents)

相關(guān)推薦:

總結(jié)

以上是生活随笔為你收集整理的python读取文件内容操作_Python 3.6 读取并操作文件内容的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。