博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python文件处理
阅读量:4685 次
发布时间:2019-06-09

本文共 1873 字,大约阅读时间需要 6 分钟。

#!/usr/bin/env python'''Created on 2017年9月15日@author:实现文件读写_判断_处理操作'''import osimport shutil#os.remove(r"H:\123.txt")#删除指定文件# chen=os.path.isfile(r"H:\1234.txt")#检验路径是否有指定文件#os.removedirs(r"c:\python")#删除目录#chen1=os.path.isdir(r"H:\wei")#检验路径是否有指定目录#chen2=os.path.split(r"H:\656.txt")#返回文件名和路径#chen3=os.stat(r"H:\656.txt")#获取文件属性#chen4=os.path.getsize(r"H:\656.txt")#获取文件大小,以字节计算#chen5=os.mkdir('chenwei')#在当前位置创建一个目录#chen6=os.makedirs(r"H:/667/chen")#指定位置创建目录#os.mknod("test.txt")#创建空文件#chen7= open(r"h:/test.txt",'r')#直接打开一个文件,如果文件不存在则创建文件#shutil.move(r"H:\656.txt",r"H:\444")#移动文件    #.............................读取文件....................'''w = file('123.txt')#shu = w.readline()#读取一行shu = w.read()#读取全部#shu = w.readlines()#把内容按行读取到list(数组)中print shuw.close()#for i in shu:#    print i'''#............................写入文件.........................'''wen= 'hrhufgrhfurufhu55565565656656'abc= open('123.txt','w')#没有文件自动新建(w表示以写的方式打开文件,r表示以读方式)abc.write(wen)#把wen写入abc,后面加换行符#abc.writelines(wen)#把wen写入abc,不加任何东西abc.close()'''#————————————判断文件中是否存在某个字符——————————————————————with open('d://12345.txt','r') as foo:#foo=open('d://12345.txt','r') #     chen=foo.readlines()#     print(chen[1])      for chen in foo.readlines():#foo.readlines()把内容按行读取到list中, chen表示循环行数        if '陈伟' in chen: print (chen)

 

  #—————————————————读取文件、去掉换行符————————————————————————————

 wo= open(r'G:\eclipse_1\web_selenium1\test\123.txt', 'r')

 shu1 = wo.readline()
 shu=shu1.strip('\n')#去掉换行符

 

 

#——————————判断str中是否包含某个字符————————————————cc='hduheudheuw陈伟额656565656565wedwdw'keke= 'yy' in ccprint (keke)#true或false#——————————判断str中是否包含某个字符(也可以判断字符串位置)————————————————string = 'helloworldchenwei565656566565656565656565'#判断某个字符是否在string变量中if string.find('chenwei')==10: #判断chenwei位置是否从10开始(10可加可不加)    print ('Exist')else:    print ('not Exist')

 

 

 

转载于:https://www.cnblogs.com/chen-xia/articles/7998884.html

你可能感兴趣的文章
JDK 1.7.0_45安装及切换
查看>>
使用Spring的HttpInvoker
查看>>
POJ 3685 - Matrix
查看>>
doubango(1)--从协议栈结构说起
查看>>
Exchange 2016 创建日程,远程服务器返回错误: (401) 未经授权
查看>>
靠谱注册
查看>>
Redis高级应用
查看>>
-对wakanda靶机的渗透之旅
查看>>
every day a practice —— morning
查看>>
使用 Nmon 监控 Linux 的系统性能
查看>>
袋鼠云研发手记 | 开源·数栈-扩展FlinkSQL实现流与维表的join
查看>>
Character Sets: Migrating to utf8mb4 with pt_online_schema_change
查看>>
[Huawei]华为交换机基础配置参考
查看>>
WebSphere MQ 入门指南【转】
查看>>
快速求素数和
查看>>
可编辑下拉框(ie6/chrome)
查看>>
Simplify Path
查看>>
redis相关资料
查看>>
zzulioj--1775-- 和尚特烦恼1——是不是素数(素数水题)
查看>>
Android学习笔记-listview实现方式之BaseAdapter
查看>>