python學習筆記(四)-文件的讀取、寫入和複製、剪切

1. 文件的讀取和寫入

四種讀取文件的方式:

  • file.read(): 讀取所有內容到一個字符串
  • file.readlines(): 讀取所有內容到list中
  • file.readline(): 只讀取一行,類似於matlab中的getl
  • with as: 推薦
# -*- coding:utf-8 -*-
#--------------------------文件讀取----------------------
print('Method 1th:')
file = open('NodeData.txt','r')  # 打開文件,下同
Content1 = file.read()           # 方法一:將所有內容讀取到字符串中
print(type(Content1),Content1)   # 字符串形式儲存
file.close()

print('Method 2th:')
file = open('NodeData.txt','r')
Content2 = file.readlines()      # 方法二:將所有內容讀取到list中
print(type(Content2),Content2)   # list形式儲存
file.close()

print('Method 3th:')
file = open('NodeData.txt','r')  
while True:
    Content3 = file.readline()   # 方法三:每次讀取一行
    print(type(Content3),Content3) # 字符串形式
    if not Content3:             # not [] = True
        break 
file.close()

print('Method 4th:')
def ReadTxt(FileName):
    with open(FileName,'r') as file:  # 方法四:推薦做法,幾個G的程序也可以
        for line in file:
            print(type(line),line)

#------------------------文件寫入------------------------
with open("Data.txt","w") as f:  # 追加,沒有該文檔則創建
    for i in range(10):          # 寫入10次
        f.write(str(2017))
ReadTxt("Data.txt")   # 調用函數進行顯示
運行結果:

Method 1th:
<class 'str'> 1 1 1.25 0.50 0.00 0.00 0.00 0.00 1.000 0.00 
2 1 0.00 0.00 0.00 0.00 0.00 0.00 1.000 0.00 
3 1 0.90 0.30 0.00 0.00 0.00 0.00 1.000 0.00 
4 1 0.00 0.00 0.00 0.00 0.00 0.00 1.000 0.00
5 1 1.00 0.35 0.00 0.00 0.00 0.00 1.000 0.00 
6 1 0.00 0.00 0.00 0.00 0.00 0.00 1.000 0.00
7 2 0.00 0.00 1.63 0.00 0.00 0.00 1.025 0.00 
8 2 0.00 0.00 0.85 0.00 0.00 0.00 1.025 0.00
9 3 0.00 0.00 0.00 0.00 0.00 0.00 1.040 0.00 
Method 2th:
<class 'list'> ['1 1 1.25 0.50 0.00 0.00 0.00 0.00 1.000 0.00 \n', '2 1 0.00 0.00 0.00 0.00 0.00 0.00 1.000 0.00 \n', '3 1 0.90 0.30 0.00 0.00 0.00 0.00 1.000 0.00 \n', '4 1 0.00 0.00 0.00 0.00 0.00 0.00 1.000 0.00\n', '5 1 1.00 0.35 0.00 0.00 0.00 0.00 1.000 0.00 \n', '6 1 0.00 0.00 0.00 0.00 0.00 0.00 1.000 0.00\n', '7 2 0.00 0.00 1.63 0.00 0.00 0.00 1.025 0.00 \n', '8 2 0.00 0.00 0.85 0.00 0.00 0.00 1.025 0.00\n', '9 3 0.00 0.00 0.00 0.00 0.00 0.00 1.040 0.00 ']
Method 3th:
<class 'str'> 1 1 1.25 0.50 0.00 0.00 0.00 0.00 1.000 0.00 

<class 'str'> 2 1 0.00 0.00 0.00 0.00 0.00 0.00 1.000 0.00 

<class 'str'> 3 1 0.90 0.30 0.00 0.00 0.00 0.00 1.000 0.00 

<class 'str'> 4 1 0.00 0.00 0.00 0.00 0.00 0.00 1.000 0.00

<class 'str'> 5 1 1.00 0.35 0.00 0.00 0.00 0.00 1.000 0.00 

<class 'str'> 6 1 0.00 0.00 0.00 0.00 0.00 0.00 1.000 0.00

<class 'str'> 7 2 0.00 0.00 1.63 0.00 0.00 0.00 1.025 0.00 

<class 'str'> 8 2 0.00 0.00 0.85 0.00 0.00 0.00 1.025 0.00

<class 'str'> 9 3 0.00 0.00 0.00 0.00 0.00 0.00 1.040 0.00 
<class 'str'> 
Method 4th:
<class 'str'> 2017201720172017201720172017201720172017

2 文件複製和粘貼

其中一種方法是用shutil包:
  • 複製:copyfile(path, filename),類似於matlab的copy
  • 剪切:move(path, filename)
# -*- coding:utf-8 -*-
#--------------------------文件操作----------------------
import shutil as FILE
import os 
FilePath = 'F:/FunctionLibrary/runpfM/NodeData.txt'  # 寫下你想複製的文件的絕對路徑
if os.path.exists(FilePath): # 判斷該路徑是否存在
    FILE.copyfile(FilePath,'NodeData.txt') # 拷貝文件
    # 將copyfile換爲move就是剪切
else:
    print('文件不存在!')
with open('NodeData.txt','r') as file:  # 方法四,顯示一下
    for line in file:
        print(type(line),line)
運行結果:

<class 'str'> 1 1 1.25 0.50 0.00 0.00 0.00 0.00 1.000 0.00 

<class 'str'> 2 1 0.00 0.00 0.00 0.00 0.00 0.00 1.000 0.00 

<class 'str'> 3 1 0.90 0.30 0.00 0.00 0.00 0.00 1.000 0.00 

<class 'str'> 4 1 0.00 0.00 0.00 0.00 0.00 0.00 1.000 0.00

<class 'str'> 5 1 1.00 0.35 0.00 0.00 0.00 0.00 1.000 0.00 

<class 'str'> 6 1 0.00 0.00 0.00 0.00 0.00 0.00 1.000 0.00

<class 'str'> 7 2 0.00 0.00 1.63 0.00 0.00 0.00 1.025 0.00 

<class 'str'> 8 2 0.00 0.00 0.85 0.00 0.00 0.00 1.025 0.00

<class 'str'> 9 3 0.00 0.00 0.00 0.00 0.00 0.00 1.040 0.00 
說明:在當地目錄下查看生成的文件(當然也可用絕對路徑把文件copy/move到別的地方)。


版權聲明:本文爲博主原創文章,未經博主允許不得轉載。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章