Learning Python Notas (cuatro) - Archivos de leer, escribir y copiar, cortar

1. lectura y escritura de archivos

Hay cuatro tipos de formas de leer el archivo:

  • file.read (): leer todo el contenido de una cadena
  • file.readlines (): He leído todos los contenidos de la lista
  • file.readline (): leer sólo una línea, similar a la GETL MATLAB
  • con como: Recomendado
# -*- 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")   # 调用函数进行显示
El resultado:

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 Archivo Copiar y pegar

Un método consiste en usar paquete shutil:
  • Copiar: copyfile (ruta, nombre de archivo), similar a la copia de MATLAB
  • Corte: movimiento (ruta, nombre de archivo)
# -*- 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)
El resultado:

<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 
Descripción: Ver genera en el archivo de directorio local (por supuesto también ser la ruta absoluta a la copia del archivo / mover a algún otro lugar).


Aviso: Este artículo es un artículo blogger original, no podrá ser reproducido sin los bloggers permitidos.



Publicado 14 artículos originales · ganado elogios 19 · Vistas a 30000 +

Supongo que te gusta

Origin blog.csdn.net/qq_24694761/article/details/79110941
Recomendado
Clasificación