python learning Notes (four) - read files, write and copy, cut

1. reading and writing files

Four kinds of ways to read the file:

  • file.read (): read all the contents to a string
  • file.readlines (): I read all the contents of the list
  • file.readline (): read only one line, similar to the matlab getl
  • with as: Recommended
# -*- 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")   # 调用函数进行显示
operation result:

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 File Copy and Paste

One method is to use shutil package:
  • Copy: copyfile (path, filename), similar to the copy matlab
  • Cut: 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)
operation result:

<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 
Description: View generated in the local directory file (of course also be the absolute path to the file copy / move to somewhere else).


Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed.



Published 14 original articles · won praise 19 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_24694761/article/details/79110941