一, 输入输出
输入: input()
输出: print()
- name=input('请输入名字')
- 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
- f=open('test.txt')
- mystr=f.read()
- f.close()
- print(mystr)
- f=open('test.txt')
- while True:
- chunk=f.read(10)
- if not chunk:
- break
- print(chunk)
- f.close
- f=open('test.txt')
- mylist=f.readlines()
- print(mylist)
- f.close()
- f=open('test.txt')
- while True:
- chunk=f.readline()
- if not chunk:
- break
- print(chunk)
- f.close()
三, 写入文件
write(): 文件对象. write(写入的内容)
追加写入: 打开文件时可以用 a 或 a + 作为参数调用 open() 然后再写入
writelines(): 文件对象.(seq) 用于写入字符串序列, seq 是个返回字符串序列 (列表, 元组, 集合等)
- f=open('test.txt','a+')
- f.write('hello')
- f.close()
- f=open('test.txt')
- for line in f:
- print(line)
- f.close()
- mylist=['hello Jack','hello Salar','hello coffee']
- f=open('test.txt','a+')
- f.writelines(mylist)
- f.close()
- f=open('test.txt')
- for line in f:
- print(line)
- f.close()
四, 文件指针
获取文件指针位置: pos = 文件对象. tell() 返回的是一个整数, 表示文件指针的位置, 打开一个文件时文件指针的位置为 0, 读写文件是, 文件指针的位置会移至读写的位置
移动文件指针: 文件对象. seek((offset,where))
offset: 移动的偏移量, 单位为字节, 正数时向文件尾方向移动, 负数时向文件头方向移动
where: 指从哪里开始移动, 0 为起始位置, 等于 1 时为当前位置移动, 等于 2 时从结束位置开始移动
- f=open('test.txt')
- print(f.tell())
- f.close()
- f=open('test.txt')
- f.seek(5,0)
- print(f.tell())
- f.close()
五, 截断文件
文件对象. truncate([size]) 从文件头开始截断文件, size 为可选参数, size 字节后的文件内容将会被截断丢掉
- f=open('test.txt','a+')
- f.truncate(5)
- f.close()
- f=open('test.txt')
- for line in f:
- print(line)
- 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)
- import os
- import shutil
- fileStats=os.stat('test.txt')
- print(fileStats)
- shutil.copy('F:\\python\\book\\ch7\\test.txt','F:\\python\\book\\ch7\\test2.txt')
- shutil.move('F:\\python\\book\\ch7\\test2.txt','F:\\python\\book\\test2.txt')
- os.rename('F:\\python\\book\\test2.txt','F:\\python\\book\\test3.txt')
- os.remove('F:\\python\\book\\test3.txt')
七, 目录编程
获取当前目录: os 模块的 getcwd() 函数
获取目录内容: os 模块的 listdir() 函数
创建目录: os 模块的 mkdir() 函数
删除目录: os 模块的 rmdir() 函数
- import os
- print(os.getcwd())
- print(os.listdir(os.getcwd()))
- os.mkdir('F:\\python\\book\ch7\\he')
- os.rmdir('F:\\python\\book\ch7\\he')
来源: http://www.bubuko.com/infodetail-2576172.html