content

10.Python的文件操作

匿名 (未验证) 提交于 2019-12-02 22:51:30
1.文件的基本操作 打开关闭文件 推荐 ) <_io.TextIOWrapper name='file.py' mode='r' encoding='UTF-8'> <_io.TextIOWrapper name='file.py' mode='r' encoding='UTF-8'> 文件打开方式 加个b的作用:以二进制的格式打开文件,进行上述操作 。 读取/写入文件 注意:文件读取完后光标移动到文尾,再读取则为空白。 读文件实例 with open ( 'mypython.py' ) as f : content = f.read () # print ( content ) # content2 = f.read () print ( content2 ) # f.seek ( 0 ) # con = f.read ( 10 ) # 10 print ( con ) # 10 con2 = f.read () # f.seek ( 0 ) cont = f.readline () # print ( cont ) # f.seek ( 0 ) conten = f.readlines () # print ( conten ) 写文件实例 with open ( 'mypython.py' , 'w' ) as f : # content = ' ' f.write (

python文件,文件夹操作

匿名 (未验证) 提交于 2019-12-02 22:51:30
首先,导入os模块 1 import os 获取当前文件所在文件夹的路径,getcwd() 更改当前目录:chdir() 创建文件夹,mkdir() 获取文件夹下所有子文件夹及文件,listdir() 判断是否是文件夹os.path.isdir() 更改文件名称,rename(old_name, new_name) 删除文件夹,rmdir() import os path = os.getcwd() print(path) # os.chdir('c:\\') # # path = os.getcwd() # print(path) contents = os.listdir() for content in contents: if os.path.isfile(content): print(content) # print("-----------------") elif os.path.isdir(content): print(content) print('----------------') contents_sub = os.listdir(content) for content_sub in contents_sub: print(content_sub) 运行结果: 1 C:\Users\Administrator\AppData\Local

python 操作excel实现替换特定内容

匿名 (未验证) 提交于 2019-12-02 22:51:30
本文介绍使用python语言,借助openyxl库来实现操作excel(xlsx)文件,实现替换特定内容的需求。 目前实现了3个小功能: 1. 全字匹配替换(mode1);(如:全字匹配 yocichen , 替换成为 yoci X chen ) 2. 部分字符匹配替换(mode2);(如:thisis yoci blog,替换成为 thisis yocichen blog) 3. 全字匹配填充(mode3);(如: yoci ,替换成为 yoci: a foolish ),用于在字符后面添加字符 源码: 1 import openpyxl 2 import re 3 import traceback 4 5 changeCells = 0 6 7 # replace the special content 8 """ 9 file: file path : str 10 mode: type of the operatoration : int 11 text: the string need to be replaceed : int or str 12 replaceText: replacement Text : int or str 13 """ 14 def changeData(file, mode, text, replaceText): 15 # load the

分享一个批量修改文件编码的python脚本

匿名 (未验证) 提交于 2019-12-02 22:51:30
分享一个自己编写的递归查找子目录,将所有cpp文件编码修改为utf-8编码格式的小脚本 1 #i!/usr/bin/env python3 2 # -*- coding:utf-8 -*- 3 import os 4 import sys 5 import codecs 6 import chardet 7 8 def convert ( filename , out_enc = "UTF-8" ): 9 try : 10 content = codecs . open ( filename , 'rb' ). read () 11 source_encoding = chardet . detect ( content )[ 'encoding' ] 12 print ( "fileencoding:%s" % source_encoding ) 13 14 if source_encoding != None : 15 content = content . decode ( source_encoding ). encode ( out_enc ) 16 codecs . open ( filename , 'wb' ). write ( content ) 17 else : 18 print ( "can not recgonize file encoding %s" %

python 文件操作

匿名 (未验证) 提交于 2019-12-02 22:51:30
读取文件:r,只读不能写,文件不存在报错 #打开文件 file_object = open ( 'log.txt' , mode = "r" , encoding = 'utf-8' )#读取: mode Ϊ r ,只读不能写,文件不存在,报错 # 读取内容, content = file_object . read () print ( content ) #关闭文件 file_object . close () 写入文件:w,只写不能读(先清空文件),文件不存在则新建 #打开文件 file_object = open ( 'looooog.txt' , mode = "w" , encoding = 'utf-8' )# mode Ϊ w , write (只写,先清空,一般用于新建文件) #写内容 file_object . write ( "李伟" ) #关闭文件file_object.close() 写入文件:a只追加不能读,文件不存在新建 #打开文件 file_object = open ( 'log.txt' , mode = "a" , encoding = 'utf-8' )#写入: a ,只在尾部追加不读,文件不存在新建 #写内容 v = file_object . write ( "您好啊" ) #关闭文件 file_object . close ( )

Python文件操作

匿名 (未验证) 提交于 2019-12-02 22:51:30
前言:在使用Python对文件进行操作时,需要先了解 绝对路径 和 相对路径 。 绝对路径:指的是从磁盘根目录到文件名,或者网上的一个完整的网络地址。例:"D:\Program Files\Tencent\QQ\Bin\QQScLauncher.exe", https://www.baidu.com/ 相对路径:指的是以当前正在处理的文件为基本路径进行操作。../表示当前文件的上一层文件夹,../../表示当前文件的上上层文件夹。例:当前文件路径为:D:\Program Files\Tencent\123.txt,要获取D:\Program Files\456.txt,可以用"../456.txt"路径表示。 tips:建议使用相对路径,因为在把我们的项目打包发送给别人时,别人直接就能运行。而如果使用绝对路径,那么还要额外发送外部的文件。 作也会有相应的差异. f = open(file_path, mode = "r", enoding="utf-8") content = f.read() f.close() print(content) # file_path:文件路径,可使用绝对路径也可使用相对路径 # mode:指定对文件进行的操作 # encoding:解码(读)或者编码(写)的格式,一般大多数都是采用utf-8格式 # close():释放文件句柄 # 还可以这样写

python爬取静态数据并存储json

匿名 (未验证) 提交于 2019-12-02 22:51:08
import requests import chardet from bs4 import BeautifulSoup import json ''' 遇到python不懂的问题,可以加Python学习交流群:1004391443一起学习交流,群文件还有零基础入门的学习资料 ''' user_agent='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0' headers={'User-Agent':user_agent} r=requests.get('http://seputu.com/',headers=headers) r.encoding=chardet.detect(r.content)['encoding'] soup=BeautifulSoup(r.text,features='html.parser') content=[] for mulu in soup.find_all(class_='mulu'): h2=mulu.find('h2') if(h2!=None): h2_title=h2.string list=[] for a in mulu

文件操作函数及光标,tell,truncate

匿名 (未验证) 提交于 2019-12-02 22:11:45
一丶文件基本操作   1.打开文件     open(文件名(路径),mode = "?",encoding = "字符集")   2.文件路径:     1.绝对路径,从磁盘的根目录寻找,或者从互联网上寻找一个路径        f = open("e:/abc/里皮.txt",mode = "r".encoding = "gbk") s = f.read() print(s) f.close()     2.相对路径,相对于当前程序所在的文件夹 ../ 返回上一级 f = open("../abc/晓峰",mode = "r",encoding="gbk") s = f.read() print(s) f.close()   3.创建新文件. 把修改后的内容写入新文件. 删除老文件. 重命名新文件 import os os.remove("")删除文件 os.rename("源文件", "新文件名") 重命名文件 for line in f: line 一行数据 with open("文件名") as f: 不需要关闭文件句柄 二丶文件操作   1.初识文件操作     1.使用python来读写文件是非常简单的操作,我们使用open()函数来打开一个文件,获取到文件句柄,然后通过文件句柄就可以进行各种各样的操作了,根据打开方式的不同能够执行的操作也会有相应的差异     2

Laravel 从入门到放弃

匿名 (未验证) 提交于 2019-12-02 22:11:45
MVC全名是Model View Controller,是模型-视图-控制器的缩写 Model是应用程序中用于处理应用程序数据逻辑的部分 View是应用程序中处理数据显示的部分 Controller是应用程序中处理用户交互的部分 app包含了用户的核心代码 booststrap包含框架启动和配置加载文件 config包含所有的配置文件 database包含数据库填充与迁移文件 public包含项目入口可静态资源文件 resource包含视图与原始的资源文件 stroage包含编译后的模板文件以及基于文件的session和文件缓存、日志和框架文件 tests单元测试文件 wendor包含compose的依赖文件 Route::match(['get', 'post']), 'match', funtion() { return 'match'; }); Route::any(['get', 'post']), funtion() { return 'any'; }); Route::get('user/{name}', funtion($name) { return $id; })->where('name', '[A-Za-z]+'); Route::get('user/{id}/{name?}', funtion($id, $name='phyxiao') { return

蚂蚁分类信息商家发布文章、商品外链及远程图片自动添加nofollow属性

匿名 (未验证) 提交于 2019-12-02 22:11:45
蚂蚁商户发布文章、商品是可以添加外链或者直接用外部图片,但是这对分类网站运营不利。 所以要对外链进行过滤,演示网站,蚂蚁分类的源码。 1、添加过滤外链函数 打开/include/global.php 添加处理函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 function tsNofollow( $content , $domain = "https://www.0516fangchan.com" ){ preg_match_all( '/href="(.*?)"/' , $content , $matches ); if ( $matches ){ foreach ( $matches as $val ){ if strpos ( $val , $domain $content = str_replace ( 'href="' . $val . '"' 'href="' . $val . , $content ); } } preg_match_all( '/src="(.*?)"/' , $content , $matches ); if ( $matches ){ foreach ( $matches as $val ){ if strpos ( $val , $domain $content = str_replace ( 'src="' .