python脚本

输入学生的姓名和成绩,打印学生成绩信息,判断成绩等级

#!/usr/bin/env python

#coding=utf-8

class Student(object):

        def __init__(self,name,score):

                self.__name = name

                self.__score = score

        def print_score(self):         //定义打印成绩信息函数

               print "%s:%s" %(self.__name,self.__score)

        def print_grade(self):          //判断成绩等级

                if self.__score >=90:

                        print "A"

                elif self.__score >=60:

                        print "B"

                else:

                        print "C"

        def get_name(self):

                print self.__name

std1 = Student("fentiao",100)

std2 = Student("hello",80)

 

std1.print_score()

std2.print_score()

 

std1.print_grade()

std2.print_grade()

#std1._Student__name  = "cat"

#std1.get_name()

 

 

打印学生std1,std2的成绩信息

#!/usr/bin/env python

#coding=utf-8

class Student(object):                      //声明student类

        def __init__(self,name,score):

                self.name = name

                self.score = score

        def print_score(self):     //定义打印成绩的函数

                print "%s:%s" %(self.name,self.score)

 

std1 = Student("zhangzhen",01)    //给student传递两个参数

std2 = Student("hejie",80)

 

std1.print_score()           //调用print_score()函数

std2.print_score()

 

 

统计一个文件中特定字符串的长度

#!/usr/bin/env python

#coding:utf-8

import re

s=raw_input("please input the str:")        //输入要统计的字符串

filename=raw_input("please input the filename:") //输入要查找的文件

fo =open(filename)

lines=len(fo.readlines())                    //统计文件的行数

fo.seek(0,0)

r=r"%s" %s                             //定义规则

count=0

for i in range(0,lines):                   //依次遍历每一行,统计每行的字符串出现个数     

      l=re.findall(r,fo.readline())         //按照规则匹配查找的字符串

      count+=len(l)                      //统计匹配到的字符串个数,将值累加到count上

print ("the %s has %d in filename" %(s,count))

 

 

 

 

file1中的a字符串用b字符串替换,并将替换后的文件重新保存在file2中

#!/usr/bin/env python

#coding=utf-8

import re

file1name=raw_input("please input the file1:")

file2name=raw_input("please input the file2:")

f1=open(file1name,"r")                      //file1文件,以只读方式打开

f2=open(file2name,"w")                      //file2文件,以只写方式打开

a=raw_input("please input you want to replaced strings:")//输入要替换的字符串

b=raw_input("please input you exchanged the strings:")   //输入用来替换的字符串

r=re.compile(r"%s"%a)                                    //定义规则匹配a字符串

file1=f1.read()                                          //读取file1文件

file1=r.sub("%s"%b,file1)                                 //用b字符串替换按照规则匹配的file1中的a字符串

print "%s replaced %s successful"

f2.write(file1)

f2.close()                                               //关闭文件file1,file2

 

f1.close()                                               

 

 

 

 

将指定目录下的文件复制到指定目录中去

#!/usr/bin/env python

#coding:utf-8

import os

list=[]               //空列表,用来保存所要复制的文件的绝对路径

back=raw_input("please input the filenameback:")      //输入要复制的文件的绝对路径

list=[back]                                              //将该路径保存在列表中      

des =raw_input("please input the destination:")      //输入文件的目的路径

desback=raw_input("please input the desback:")

os.mkdir(des+desback)                       //创建该目的路径

command="cp %s %s " %(" ".join(list),des+desback)     //执行复制命令

if os.system(command)==0:                 //如果os.system()返回值位0,则表示复制成功

      print "cp successful"

else:                            //否则,操作失败

     print "operator failed"     

 

 

 

 

遍历目录,递归取出目录下的所有文件。

#!/usr/bin/env python

#coding:utf-8

import os

def dirlist(path):

allfilename=[]

flist=os.listdir(path)

for filename in flist:

   filepath=os.path.join(path,filename)

   if os.path.isdir(filepath):

       dirlist(filepath)

   else:

       allfilename.append(filepath)

       return allfilename

print dirlist("/a")

print dirlist("/a/c")


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章