python脚本练习
来源:互联网 发布:安顺行知高中 编辑:程序博客网 时间:2024/06/13 13:10
1.比较两个文件不同,返回第一处的不同
#!/usr/bin/env pythonimport osfilename1 = raw_input("Please input a filename1:")filename2 = raw_input("Please input a filename2:")if not os.path.isfile(filename1):print "ERROR:%s is not a file!"%filename1exit(1);elif not os.path.isfile(filename2):print "ERROR:%s is not a file!"%filename2exit(1)f1 = open(filename1,"r")f2 = open(filename2,"r")file1 = f1.readlines()file2 = f2.readlines()f1.close()f2.close()k = 0if file1 == file2:print "The two files are the same!"exit(0)while True :if k <= len(file1) and k <= len(file2) and file1[k] == file2[k]:k += 1else :breakm = 0while True:if m <= len(file1[k]) and m <= len(file2[k]) and file1[k][m] == file2[k][m]:m += 1else :breakprint "Different place:row=%d,col=%d"%(k+1,m+1)print "file1:%d %s"%(k+1,file1[k])print "file2:%d %s"%(k+1,file2[k])
2.获取合法的url
#!/usr/bin/env python#encoding:utf-8import reimport osdef menu():print '''(R)ead url from file(I)nput url from keyboard(E)xit'''while True:choice = raw_input(">>>").upper()if choice == 'R':Read()elif choice == 'I':Input()elif choice == 'E':exit(0)elif choice == '':continueelse :print "ERROR:unknow your input!"def Read():filename = raw_input("Please input filename:")if not os.path.isfile(filename):print "ERROR:%s is not file!"%filenameexit(1)f = open(filename,"r")filedata = f.readlines()print "url:"for data in filedata:if len(re.findall(r,data)) > 0:print re.findall(r,data)def Input():data = raw_input("Please input data:")print "url:"if len(re.findall(r,data)) > 0:print re.findall(r,data)r = re.compile(r"www\.\w+\.com|www\.\w+\.edu|www\.\w+\.net")menu()
0 0
- Python脚本练习一
- python脚本练习
- python脚本练习
- Python脚本练习-登录
- python 基础脚本练习
- python脚本练习
- Python脚本练习
- 脚本练习
- 脚本练习
- 【脚本练习】shell脚本
- python argparse模块小练习--Python脚本实现linux命令的功能
- python练习
- python练习
- Python练习
- python练习
- python练习
- #python#练习
- python 练习
- 纯css做成的小鹿地球月亮闪烁的星星
- 面试答案
- Maven_5个必须掌握的maven命令
- Linux_压缩解压详解
- Spring_<context:component-scan>
- python脚本练习
- Tomcat_CATALINA_BASE与CATALINA_HOME的区别
- 部分错误码详解
- 块元素(block)的特点
- iOS控件-UICollectionView基本使用详解(OC)
- Maven_pom详解
- 使用lua脚本和jedis实现redis的hmsetnx命令,操作hash表时不覆盖原有数据
- Android开发中退出程序几种方法
- Linux_nohup