Python文件操作相关方法

本文详细介绍了Python中进行文件操作的各种方法,包括打开、读取、写入、追加、关闭文件等核心操作,以及文件对象的使用技巧和异常处理。通过实例解析,帮助读者深入理解Python文件I/O。
摘要由CSDN通过智能技术生成

Python文件操作相关方法

File I/O
open(path, falg[,encoding],[errors])
flag:打开方式
r - read only
rb - open file in bite and read only
r+ - open a file and can read and write
w - open a file and write only
wb - write only in bite
w+ -- write and read
a -- append content in a opened file

#windows的path要将\转成/如果前面不加r
#path = 'C:/Users/qiuy/PycharmProjects/pythonProject/file.txt'
#加r后path要用双引号
#path = r"C:\Users\qiuy\PycharmProjects\pythonProject\file.txt"
#try:
    #f = open(path,'r')

    #修改文件描述符的位置,从第x个字节开始操作
    #f.seek(10)
    #1.read all the content of the file
    #file_all = f.read()
    #read的参数是字节数 
    #file_part = f.read(100) 
    #print(file_part)
    #2.read part of the file
    #file_line = f.readline()
    #print('readline',file_line)
    #file_lines = f.readlines()
    #print('readlines',file_lines)
    

    #write into a file
 #   f = open(path,&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值