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到别的地方)。