Python基础10—I/O编程

一、输入输出

  ①输入:input()

  ②输出:print()

1 name=input('请输入名字')
2 print(name)

二、文件操作

  ①打开文件:文件对象=open(文件名,访问模式,buffering)

    文件名:指定要打开的文件,通常需要包含路径,可以是绝对路径也可以是相对路径

    buffering:可选参数,指定文件所采用的缓冲方式,buffering=0,不缓冲;buffering=1,缓冲一行;buffering>1,使用给定值作为缓冲区大小

    访问模式:用于指定打开文件的模式,访问模式的参数表如下

可取值 含义
r 以读方式打开
w 以写的方式打开,此时文件内容会被前清空,如果文件不存在则会创建文件
a 以追加的模式打开,从文件末尾开始,必要时创建新文件
r+、w+ 以读写模式打开
a+ 追加的读写模式打开
rb 以二进制读模式打开
wb 以二进制写模式打开
rb+、wb+、ab+ 以二进制读写模式打开

  ②关闭文件:文件对象.close()

  ③读取文件:

    Ⅰ:str=文件对象.read([b])

    可选参数[b]:指定读取的字节数,默认读取全部

    Ⅱ:list=文件对象.readlines()读取的内容返回到字符串列表中

    Ⅲ:str=文件对象.readline()一次性读取文件中的所有行

    Ⅳ:使用in关键字:for line in 文件对象:处理行数据line

 1 f=open('test.txt')
 2 mystr=f.read()
 3 f.close()
 4 print(mystr)
 5 
 6 f=open('test.txt')
 7 while True:
 8     chunk=f.read(10)
 9     if not chunk:
10         break
11     print(chunk)
12 f.close
 1 f=open('test.txt')
 2 mylist=f.readlines()
 3 print(mylist)
 4 f.close()
 5 f=open('test.txt')
 6 while True:
 7     chunk=f.readline()
 8     if not chunk:
 9         break
10     print(chunk)
11 f.close()

三、写入文件

  ①write():文件对象.write(写入的内容)

  ②追加写入:打开文件时可以用a或a+作为参数调用open()然后再写入

  ③writelines():文件对象.(seq)  用于写入字符串序列,seq是个返回字符串序列(列表、元组、集合等)

1 f=open('test.txt','a+')
2 f.write('hello')
3 f.close()
4 f=open('test.txt')
5 for line in f:
6     print(line)
7 f.close()
1 mylist=['hello Jack','hello Salar','hello coffee']
2 f=open('test.txt','a+')
3 f.writelines(mylist)
4 f.close()
5 f=open('test.txt')
6 for line in f:
7     print(line)
8 f.close()

四、文件指针

  ①获取文件指针位置:pos=文件对象.tell()  返回的是一个整数,表示文件指针的位置,打开一个文件时文件指针的位置为0,读写文件是,文件指针的位置会移至读写的位置

  ②移动文件指针:文件对象.seek((offset,where))  

    offset:移动的偏移量,单位为字节,正数时向文件尾方向移动,负数时向文件头方向移动

    where:指从哪里开始移动,0为起始位置,等于1时为当前位置移动,等于2时从结束位置开始移动  

1 f=open('test.txt')
2 print(f.tell())
3 f.close()
4 f=open('test.txt')
5 f.seek(5,0)
6 print(f.tell())
7 f.close()

五、截断文件

  文件对象.truncate([size])  从文件头开始截断文件,size为可选参数,size字节后的文件内容将会被截断丢掉

1 f=open('test.txt','a+')
2 f.truncate(5)
3 f.close()
4 f=open('test.txt')
5 for line in f:
6     print(line)
7 f.close()

六、其他操作

  ①文件属性: 使用os模块的stat()函数一科获取文件的属性,创建时间、修改时间、访问时间、文件大小等属性(使用前要导入模块:import os)

  ②复制文件:使用shutil模块的copy()函数即可,函数原型为:copy(src,dst)表示为把源文件src复制为dst

  ③移动文件:使用shutil模块的move()函数即可,函数原型为:move(src,dst)

  ④重命名文件:使用os模块的rename()函数即可,函数原型为:os.rename(原文件名,新文件名)

  ⑤删除文件:使用os模块的remove()函数即可,函数原型为:os.remove(src)

1 import os
2 import shutil
3 fileStats=os.stat('test.txt')
4 print(fileStats)
5 
6 shutil.copy('F:\\python\\book\\ch7\\test.txt','F:\\python\\book\\ch7\\test2.txt')
7 shutil.move('F:\\python\\book\\ch7\\test2.txt','F:\\python\\book\\test2.txt')
8 os.rename('F:\\python\\book\\test2.txt','F:\\python\\book\\test3.txt')
9 os.remove('F:\\python\\book\\test3.txt')

七、目录编程

  ①获取当前目录:os模块的getcwd()函数

  ②获取目录内容:os模块的listdir()函数

  ③创建目录:os模块的mkdir()函数

  ④删除目录:os模块的rmdir()函数

1 import os
2 print(os.getcwd())
3 print(os.listdir(os.getcwd()))
4 os.mkdir('F:\\python\\book\ch7\\he')
5 os.rmdir('F:\\python\\book\ch7\\he')

猜你喜欢

转载自www.cnblogs.com/caicoffee/p/8955092.html