zoukankan      html  css  js  c++  java
  • python学习 (三十四) Python文件操作

    1 写文件

    my_list = ["1", "2", "3"]
    
    my_file = open("myfile.txt", "w")
    
    for item in my_list:
        my_file.write(item + "
    ")            
    my_file.writelines(my_list) // 写多个 my_file.close()

    2  读文件

    my_file = open("myfile.txt", "r")
    print(my_file.read())                    // 一次性读取整个文件
    my_file.close()
    
    my_file = open("myfile.txt", "r")
    print(my_file.readline())                // 读一行
    print(my_file.readline())                // 再读下一行
    my_file.close()

     3  循环读取文件

    my = open("myfile.txt")
    
    for line in my:
        print(line + " aaa ")

     4 with ... As ...

    with open("myfile.txt", "w") as with_as_writ:
        with_as_writ.write("aaa")
    
    
  • 相关阅读:
    ASP.NET初识4
    属性
    ASP.NET初识4
    ACCP6.0第九章练习
    ASP.NET初识1
    鼠标指针含义
    ASP.NET初识2
    第三部分
    ASP.NET初识3
    ASP.NET初识5
  • 原文地址:https://www.cnblogs.com/liufei1983/p/9866429.html
Copyright © 2011-2022 走看看