• Python 文件读写


    #基于Python36

    1.Python从文件输入输出,in.txt和out.txt文件在代码的当前目录:

    1 import sys
    2 sys.stdin=open('in.txt','r')
    3 sys.stdout=open('out.txt','w')

     2.Python输出到在指定目录的文件test_result.txt:

    1 #读写模块
    2 import sys
    3 outfile_path = "D:\RUN\Classification\Linear Classifiers\result"  
    4 outfilename = "test_reasult.txt"
    5 print (outfile_path + "\" +outfilename)
    6 sys.stdout = open(outfile_path + "\" +outfilename,"w")

    3.附上读写选项:

     1 #文件操作示例 open()
     2 #open("路径 + 文件名",”读写参数")
     3 f=open('filepath','w')
     4  
     5 #读写参数
     6 # r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件
     7 #常用读写模式
     8 #如:'rb','wb','r+b'等等
     9 #读写模式的类型有:
    10 #rU 或 Ua 以读方式打开, 同时提供通用换行符支持 (PEP 278)
    11 #w      以写方式打开,
    12 #a      以追加模式打开 (从 EOF 开始, 必要时创建新文件)
    13 #r+     以读写模式打开
    14 #w+     以读写模式打开
    15 #a+     以读写模式打开
    16 #rb     以二进制读模式打开
    17 #wb     以二进制写模式打开
    18 #ab     以二进制追加模式打开
    19 #rb+    以二进制读写模式打开
    20 #wb+    以二进制读写模式打开
    21 #ab+    以二进制读写模式打开
    22 #W      若文件存在,首先要清空,然后重新创建文件
    23 #a      把所有的数据追加到文件的尾部,即使seek指在其他的位置,如果文件不存在,则重新创建
  • 相关阅读:
    C++ 重载运算符
    线段树
    矩阵的构造
    矩阵快速幂
    Fibnoccia 数列简单题
    权值线段树
    .net System.Net.Mail 之用SmtpClient发送邮件Demo
    poj-3087 Shuffle'm Up
    hdu-1548 A strange lift
    scu-4440 rectangle (非原创)
  • 原文地址:https://www.cnblogs.com/cnXuYang/p/7011463.html
Copyright © 2020-2023  润新知