python之文件操作

# f = open('a.txt', encoding='utf-8',mode='r')
# content = f.read()
# print(content)
# f.close()

  


'''f 变量 f_obj, f_handler, f_h, fh ,文件句柄。open() python的内置函数(内部调用的是windows的系统命令)。

1,打开文件,产生文件句柄。

2,对文件句柄进行操作。

3,关闭文件句柄。

  • r *****
  • rb ***
  • r+ ***** r+b
  • read()
  • read(n)
  • readline()
  • readlines()
  • for 循环


  • w ***
  • wb **
  • w+
  • w+b

追加

  • a ***
  • ab
  • a+
  • a+b

文件其他操作方法

  • read()
  • close()
  • seek() *****
  • tell() *****
  • readab() ****
  • writable() ****
  • truncate

文件的改 *****

'''

# 读 r 对于r模式,mode可以默认不写。
#1,全部读取
# f = open('a.txt',encoding='utf-8',mode='r')
# print(f.read())  #全部读取
# f.close()


#2,read(n)
# f = open('a.txt',encoding='utf-8',mode='r')
# conten = f.read(10)   #r模式,按字符读取
# print(conten)
# f.close()




#3,按行读取
# f = open('a.txt',encoding='utf-8',mode='r')
#
# print(f.readline())  #
# print(f.readline())  #
# print(f.readline())  #
# print(f.readline())  #
# f.close()


#4,按行读取,返回一个列表
# f = open('a.txt',encoding='utf-8',mode='r')
# conten = f.readlines()
# print(conten)
# f.close()

#5,for循环
# f = open('a.txt',encoding='utf-8',mode='r')
# for line in f:
#     print(line.strip())
# f.close()
#


'''
#报错原因
#1, 编码不一致,存储文件时编码与打开文件时编码不一致。
#2,路径问题
    在路径的最前面, +r
    每个 变成 \
    
绝对路径:d:a.txt 从根目录开始找
相对路径:当前目录:当前文件夹。
'''

# rb
# 文件操作中凡是带b字母,都是与非文字类文件相关的。
# f = open('a.jpg',mode='rb')
# content = f.read(9)   #rb模式, n按照字节读取



# r+  读写:先读后追加。
# f = open('文件操作2',encoding='utf-8', mode='r+')
# content = f.read()
# print(content)
# f.write('66666')
# f.close()

# 不读直接写,直接从开始覆盖。
# f = open('文件操作2',encoding='utf-8', mode='r+')
#
# f.write('深圳你好')
# f.close()





# 写
# w
#  没有文件,创建文件也要写。
# 有文件  先清空,后写入。
# f = open('文件操作2',encoding='utf-8',mode='w')
# f.write('深圳市南山区')
# f.close()


# wb 省略编码模式
# f = open('a.jpg',mode='rb')
# content = f.read()

# f1 = open('a2.jpg',mode='wb')
# f1.write(content)
# f.close()
# f1.close()




# 追加

# a
#  没有文件,创建文件也要写。
# 有文件  直接在文件的最后面追加



# f = open('文件操作2',encoding='utf-8',mode='a')
# f.write('广州天河区')
# f.close()



# w+ :写读
# f = open('文件操作2',encoding='utf-8',mode='w+')
# f.write('深圳市南山区')
# f.seek(0,2)  #seek 按照字节调整光标
# content = f.read()
# print(content)
# f.close()


# seek *****
# 将光标调整到开始:seek(0)。
# 光标从当前位置开始调整:seek(0,1)
# 将光标调整到结尾,seek(0,2)


# f = open('文件操作2',encoding='utf-8',mode='r')
# f.seek(0,2)
# print(f.read())
# f.close()



# tell 告知光标的位置  *****
f = open('文件的改',encoding='utf-8')
f.seek(0,2) # 0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。
print(f.tell())
f.close()

# truncate 在writeable模式下进行截取。 在r+模式
# r+ a+ 不能在w模式下使用。对原文件进行截取。




# 在一定时间内关闭文件。
# 1,主动关闭文件句柄
# 2,开启多个文件句柄。
# with open('文件操作2',encoding='utf-8') as f,
#         open('wenjian',encoding='utf-8',mode='w') as f2:
#     print(f.read())
#     f2.write('6666')
#     print(f2)


'''
# 文件的改的操作
1, 以读的模式打开原文件,产生一个文件句柄f1
2,以写的模式创建一个新文件,产生一个文件句柄f2
3,读取原文件内容,进行修改,并将修改后的文件写入新文件。
4,将原文件删除。
5,将新文件重命名成原文件。

'''
import os
with open('文件的改', encoding='utf-8') as f1,
    open('改', encoding='utf-8', mode='w') as f2:
    old_content = f1.read()
    new_content = old_content.replace('alex', 'SB')
    f2.write(new_content)
os.remove('文件的改')
os.rename('改', '文件的改')


# import os
# with open('文件的改', encoding='utf-8') as f1,
#     open('改', encoding='utf-8', mode='w') as f2:
#     for line in f1:
#         new_line = line.replace('SB', 'alex')
#         f2.write(new_line)
# os.remove('文件的改')
# os.rename('改', '文件的改')
#

# import os
# with open('alex的深度剖析', encoding='utf-8') as f1,
#     open('alex的深度解析.bak',encoding='utf-8',mode='w') as f2:
#     old_content = f1.read()
#     new_content = old_content.replace('alex','SB')
#     f2.write(new_content)
# os.remove('alex的深度剖析')
# os.rename('alex的深度解析.bak', 'alex的深度剖析')


# import os
# with open('alex.txt', encoding='utf-8') as f1,
#     open('alex2.txt',encoding='utf-8',mode='w') as f2:
#     for line in f1:
#         new_line = line.replace('SB','alex')
#         f2.write(new_line)
# os.remove('alex.txt')
# os.rename('alex2.txt', 'alex.txt')

  

原文地址:https://www.cnblogs.com/eaoo/p/9470778.html