zoukankan      html  css  js  c++  java
  • [Python基础]006.IO操作

    IO操作


    输入输出

    输入输出方法都是Python的内建函数,并且不需要导入任何的包就可以使用。

    print

    简单控制台输出方法 print ...

    print 'content'

    raw_input

    简单的控制台输入方法 raw_input(显示的内容....)

    num = raw_input('Please input a number:')   // 输入一个数字
    print num

    input

    同样是输入方法,与raw_input不同的是:raw_input将所有输入作为字符串,返回字符串;而input要求输入的内容是一个合法的Python表达式。如在输入字符串时,要用引号包裹字符串,输入"xxx"

    content = input('Please input a string:')   //输入一个字符串
    print content

    文件

    Python通过一个内建的file类型来操作一个文件。

    打开文件

    open(文件路径, 读写模式) 或者 file(文件路径, 读写模式) 方法。二者没有区别,都会返回一个file

    fr = open('test.txt', 'r')  # 读模式打开text.txt
    fw = file('test.txt', 'w')  # 写模式打开text.txt

    读写模式类型有:

    • r 读模式(默认模式,可以不写)
    • w 写模式
    • a 追加模式
    • r+ 读写模式
    • w+ 读写模式
    • a+ 读写模式
    • rb 二进制读模式
    • wb 二进制写模式
    • ab 二进制追加模式
    • rb+ 二进制读写模式
    • wb+ 二进制读写模式
    • ab+ 二进制读写模式

    关闭文件

    打开文件后要关闭文件。close(文件)

    f = open('test.txt')
    # 操作文件...
    f.close()

    读文件

    使用file对象的read等方法进行文件的读取。

    • read 读取
    • readline 读取一行文件
    • readlines 读取文件所有行

    代码

    f = open('test.txt', 'r')
    print f.read(10)                # 读取10个字节的数据
    print f.read(100)               # 再读取后100个字节的数据
    f.close()
    
    # 第一种读取方式
    f1 = open('test.txt', 'r')
    print f1.read()                 # 不带参数时,读取文件的所有内容
    f1.close()
    
    # 第二种读取方式
    f2 = open('test.txt')
    line = f2.readline()            # 读取文件第一行
    while line:
        # 不断读取文件下一行,直到line为空值
        print line
        line = f2.readline()        # 读取文件的下一行
    f2.close()
    
    # 第三种读取方式
    f3 = open('test.txt')
    lines = f3.readlines()          # 讲文件以行为单位做成一个数组返回
    for line in lines:
        print line
    f3.close()

    readlinereadlines都不会删除行结尾符
    在没有足够内存一次读取整个文件时,就要使用readline

    写文件

    使用file对象的write等方法进行文件的写入。

    • write 写入文件
    • writelines 写入多行

    代码

    # 覆盖写入
    f = open('test.txt', 'w')
    f.write('write it to your file.')   # 写入
    f.close()
    
    # 追加写入
    f = open('test.txt', 'a')
    f.write('write it to your file.')   # 写入
    f.close()
    
    # 写入多行
    f = open('test.txt', 'w')
    content = ['aaaa
    ', 'bbbb
    ', 'cccc
    ', 'dddd
    ']
    f.writelines(content)               # 写入多行
    f.close()

    文件指针

    文件指针的操作

    • seek 移动文件指针到不同的位置
    • tell 显示文件当前指针的位置

    代码

    f = open('test.txt')
    
    f.readline()
    print f.tell()              # 文件指针位置改变
    f.seek(0)                   # 文件指针回到起始位置0
    print f.tell()
    print f.readline()          # 始终打印第一行

    实例

    下面是一个简单的文件操作实例。

    # -*- coding: utf-8 -*-
    
    import os
    
    while True:
        filepath = raw_input('输入要读取的文件路径:')
        if not os.path.exists(filepath):        # 判断文件是否存在
            print '文件不存在'
            continue
        f = open(filepath)
        print f.read()
        f.close()

     

    本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 
    转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4544859.html

     

  • 相关阅读:
    POJ 1401 Factorial
    POJ 2407 Relatives(欧拉函数)
    POJ 1730 Perfect Pth Powers(唯一分解定理)
    POJ 2262 Goldbach's Conjecture(Eratosthenes筛法)
    POJ 2551 Ones
    POJ 1163 The Triangle
    POJ 3356 AGTC
    POJ 2192 Zipper
    POJ 1080 Human Gene Functions
    POJ 1159 Palindrome(最长公共子序列)
  • 原文地址:https://www.cnblogs.com/superdo/p/4544859.html
Copyright © 2011-2022 走看看