#!/usr/bin/env python ''' Created on 2017年9月15日 @author:实现文件读写_判断_处理操作 ''' import os import 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 shu w.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 cc print (keke)#true或false #——————————判断str中是否包含某个字符(也可以判断字符串位置)———————————————— string = 'helloworldchenwei565656566565656565656565' #判断某个字符是否在string变量中 if string.find('chenwei')==10: #判断chenwei位置是否从10开始(10可加可不加) print ('Exist') else: print ('not Exist')