zoukankan      html  css  js  c++  java
  • Python的I/O操作

    1.读取键盘输入

    msg = raw_input("Please enter :")
    print "you input ",msg
    
    #可接受Python表达式作为输入
    msg2 = input("请输入:")
    print "你输入了",msg2

    2.读文件

    fo = open("foo.txt","r")
    print fo.read()
    fo.close()

    3.写文件

    fo = open("foo.txt","w")
    fo.write("www.cnblogs.com")
    fo.close()

    4.重命名

    import os
    #将foo.txt重命名为doo.txt
    os.rename("foo.txt","doo.txt")

    5.删除文件

    import os
    os.remove("doo.txt")

    二、Python里的目录

    1.创建一个文件夹

    import os
    os.mkdir("folder")

    2.获取当前目录

    import os
    print os.getcwd()

    3.删除目录

    import os
    os.rmdir("folder")

    三、储存器

    使用它可以在一个文件中储存任何Python对象,之后又可以完整无缺地取出来,这被称为持久的储存对象

    import cPickle as p
    #import pickle as p
    
    shoplist = ['apple','mango','carrot']
    f = file('aa.data','w')
    p.dump(shoplist,f)
    f.close()
    print id(shoplist)
    print shoplist[0]
    del shoplist
    f = file('aa.data')
    storedlist = p.load(f)
    print id(storedlist)

    aa.data的内容

    (lp1
    S'apple'
    p2
    aS'mango'
    p3
    aS'carrot'
    p4
    a.
  • 相关阅读:
    玩家上线
    小退
    GS发包到MS
    share初始化
    3 水仙花数
    The left-hand side of an assignment must be a variable,代码中使用了中文的字符
    Mac Sublime Text 浏览器 设置快捷键 让html文件在浏览器打开
    2 质数求解
    1 斐波那契的兔子
    18 赛手的名单
  • 原文地址:https://www.cnblogs.com/kimisme/p/5572348.html
Copyright © 2011-2022 走看看