您的位置:首页 > 编程语言 > Python开发

python读书笔记二、文件基本操作

2017-10-21 01:57 573 查看
##!_*_coding:utf-8_*_
# 文件read操作
#############################################################
##一、文件打开路径,在windows中将\换成/
'''
OSError: [Errno 22] Invalid argument:报错处理 http://blog.csdn.net/m0_37667602/article/details/73350528 open函数定义
<variable>=open(<name>,<mode>)
在此处,name为磁盘文件名
格式应为路径+文件名+后缀。例如d:/abc.exe ‘d:/’为盘符,‘abc’为文件名,‘.exe’就是后缀。
所以 infile=open("C:\number.txt","r")
应该改为infile=open("c:/number.txt","r")
'''
#file_path = 'D:/dhd/python/19期\day5/11.txt'
#file_path = 'D:/dhd/vim.txt'

# with open(file_path,encoding="utf-8") as f:
#     for line in f:
#         print(line.rstrip())
#############################################################
##二、逐行读取
# file_name = "11.txt"
# with open(file_name, encoding="utf-8") as f:
#     for line in f:
#         #rstrip:去除换行符
#         print(line.rstrip()) #逐行读取所有的行
#         if line.startswith("i"):
#             #print("\033[31;1mred\033[0m")
#             print("\033[31;1m %s \033[0m" % line.title())
#############################################################
##三、read
#with open('22.txt') as f:
# # read方法会读取文件的所有内容,将其作为字符串存储在变量contents中
# contents = f.read()
# print(contents)
#############################################################
#四、使用文件的内容
# file_name = "22.txt"
# with open(file_name, encoding="utf-8") as f:
#     #创建一个包含文件各行的列表
#     lines = f.readlines()
#
# pi_string = ''
# for line in lines:
#     #print(line)     #打印每一行
#     #print(line.rstrip())  #去除每一行后面的换行符
#     #循环将各行加入到pi_string变量,即将多行文本打印成一行,且去除换行符
#     #pi_string += line.rstrip()
#     #循环将各行加入到pi_string变量,即将多行文本打印成一行,且删除每一行左边的空格
#     pi_string += line.strip()
#
# print(pi_string)
# print(pi_string[:12] + "......")   #只打印12个字符,即小数点后10位
# print(len(pi_string))   #len包含所有的字符统计

'''
注意,python讲其中所有的文本都解读成字符串,
如果将其中的数字当做数值来用,
就需要init()转换为整数,float()转换为浮点数.
'''
#############################################################
##五、圆周率中包含你的生日吗?
# file_name = 'pai.txt'
#
# with open(file_name) as f:
#     lines = f.readlines()
#
# pi_string = ''
# for line in lines:
#     pi_string += line.strip().replace(" ",'') #去除文本中的空格
#
# #print(pi_string[:12])
#
# birthday = input(">>:").strip()
# if birthday in pi_string: #当连续全匹配时候才匹配
#     print("in")
# else:
#     print("not in")


##!_*_conding:uft-8_*_
# 文件的write操作
#############################################################
#一、写入文件
#print("\033[30;1mI Love Xiao Yu !!! \033[0m\033[31;1m我爱小雨!!!\n"*100)

#file_name = 'prog.txt'

# with open(file_name,'w',encoding='utf-8') as f:
#     f.write("i love xiao yu !!! 我爱小雨!!!\n"*3)
#
# with open(file_name,encoding="utf-8") as n_f:
#     for lines in n_f:
#         #pass
#         print(lines.strip().title())
#############################################################
##二、追加内容到文件
# file_name = 'prog.txt'
#
# with open(file_name,'a',encoding="utf-8") as f:
#     f.write("i love xiao yu very much !!!\n" *3)
#     f.write("我非常的想小雨乖乖!!!\n"*3)
#
# with open(file_name,encoding="utf-8") as f:
#     for lines in f.readlines():
#         print(lines.strip().title())
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python入门