day05

一.文件操作

1.什么是文件

  • 文件是操作系统为用户提供的一个读写硬盘的虚拟单位,文件的基本操作就是读写

2.为什么要有文件

  • 内存无法永久保存数据,我们想要永久保存数据,就需要把文件保存到硬盘当中,而操作文件可以实现对硬件的操作

3.如何使用文件

  • a.打开文件

    with open (url,'mode',encoding='utf8') as f:
    	f.read()
    
  • b.读写文件

    with open (url,'r',encoding='gbk') as f:
        f.read()
    
  • c.关闭文件

    f.close()
    

文件程序

# -*- coding: utf-8 -*-
# @Time    : 2019/8/27 14:31
# @Author  : yqliu

import jieba
import wordcloud
import imageio

#找到文件路径
file_path=r'E:\code\20190821\0827\file.txt'
#打开文件
# with open(file_path,'a+',encoding='utf8') as fr:
#     print (fr.writable(),fr.readable())
#     # data=fr.read()
#     # fr.write("jjjj")
#     # fr.flush()
#     print ("hhhh")
#     fr.write("aaaa")
#     fr.flush()
#     data=fr.read()
#
# print (data)


#只读方式r
# with open(file_path,'r',encoding='gbk') as f:
#     print (f.readable(),f.writable())
#     data=f.read()
#
# print (data)

#只写方式w
# with open(file_path,'w',encoding="gbk") as f:
#     print (f.writable(),f.readable())
#     f.write("啧啧啧啧啧啧做做做做做做做做做做")
#
#追加方式a

# with open(file_path,'a',encoding='gbk') as f:
#     print (f.readable(),f.writable())
#     for i in range(15):
#         f.write("人多的多多多多多多")



with open(file_path,'r',encoding='utf8') as f:
    print (f.readable(),f.writable())
    data=f.read()

res=jieba.lcut(data)
print (res)

res2=' '.join(res)
print (res2)

mk=imageio.imread('1.png')
w=wordcloud.WordCloud(font_path=r'C:\Windows\Fonts\simkai.ttf',background_color='white',mask=mk)
w.generate(res2)

w.to_file('lyq.png')

输出结果

posted @ 2019-08-27 21:12  ztzdhbg  阅读(92)  评论(0编辑  收藏  举报