Chinaunix首页 | 论坛 | 博客
  • 博客访问: 265464
  • 博文数量: 113
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1044
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-15 16:09
文章分类

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: Python/Ruby

2015-10-18 13:02:24


点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #coding: utf-8
  3. #python 浅拷贝指,对引用的拷贝,只拷贝父对象
  4. # 和深拷贝就是对对象的资源的拷贝
  5. import copy
  6. a=[1,2,3,['a','b','c']]
  7. b=a
  8. c=copy.copy(a)
  9. a.append('e')
  10. print a
  11. print b
  12. print c
  13. d=copy.deepcopy(a)
  14. print d

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #coding: utf-8
  3. #文件与目录
  4. #目录分析器
  5. #杀毒软件
  6. #系统垃圾清理工具
  7. #python 进行文件读写的函数open
  8. #r只读
  9. #r+读写
  10. #w+ 读写,先删除文件,再添加内容
  11. #a写入,在文件末尾 追加新的内容
  12. #a+ 读写,在文件末尾追加 新的内容
  13. fo=open('/root/server/test.c')
  14. print fo.read()
  15. fo.close()
  16. #file
  17. f1=file('/root/server/test.c')
  18. print f1.read()
  19. f1.close()
  20. #input file
  21. f1=open('/root/server/test.c','r+')
  22. f1.write('good \n csvt')
  23. f1.close()
  24. ##############

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #coding: utf-8
  3. #文件对象方法
  4. f1=open('test.txt')
  5. s1=f1.read()
  6. print s1
  7. f1.close()
  8. #######
  9. for i in open('test.txt'):
  10.     print i
  11. #########
  12. #readline按行读取 string=File.readline([size])
  13. f1=open('test.txt')
  14. print f1.readline()
  15. f1.close()
  16. #########readlines
  17. f1=open('test.txt')
  18. print f1.readlines()
  19. ####f1.next() 返回当前行,并将指针指向下一行
  20. f1=open('test.txt')
  21. print f1.next()
  22. print f1.next()
  23. print f1.next()
  24. ###########write writelines()多行写入,也就是写入一个列表
  25. l=['\none\n','two\n','three\n']
  26. f1=open('test.txt','a')
  27. f1.writelines(l)
  28. f1.close()
  29. ########f1.seek()指针位移
  30. f1=open('test.txt','a')
  31. print f1.read()
  32. #将指针移到开头
  33. f1.seek(0,0)
  34. ###将指针移到末尾
  35. f1.seek(0,2)

  36. print f1.read()
  37. #########f1.seek(偏移量,选项)
  38. #选项=0时,在开头, 选项=1,在当前位置,选项=2偏移到末尾
  39. ####f1.flush()提交更新
  40. f1=open('test.txt','a')
  41. f1.writelines(l)
  42. f1.writelines(l)
  43. f1.flush()
  44. f1.close()

点击(此处)折叠或打开

  1. #文件的查找和替换
  2. #查找文件中的hello,统计个数
  3. import re
  4. f1=open('test.txt')
  5. count=0
  6. for s in f1.readline():
  7.     li=re.findall("hello",s)
  8.     if len(li)>0:
  9.         count=count+len(li)
  10. print "Search "+count+"hello"
  11. f1.close()
  12. ###文件替换hello csvt 保存到文件2,t中
  13. import re
  14. fp=file("a.t","r")
  15. fp2=file("a2.t","w")
  16. for s in f1.readlines():
  17.     fp2.write(s.replace("hello","csvt"))
  18. fp1.close()
  19. fp2.close()

点击(此处)折叠或打开

  1. #OS模块
  2. #1.目录创建操作 os.mkdir('/root/csvt')
  3. import os
  4. os.mkdir('test')
  5. #os 常用函数操作
  6. #mkdir(path,[,mode=0777])
  7. #2.listdir(path) 获得目录下的所有文件
  8. os.listdir('.')
  9. #makedirs(name,mode=511) 多级目录
  10. os.makedirs('a/b/c')
  11. os.removedirs('a/b/c')
  12. #os,rmdir('test')
  13. #getcwd() ==pwd
  14. os.getcwd()
  15. #chdir(path)切换目录
  16. os.chdir('/')
  17. #walk(top,topdown=True ,onerror=None)

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #coding:utf-8    
  3. import os


  4. # def dirlist(path):
  5.     # filelist=os.listdir(path)
  6.     # fpath=os.getcwd()
  7.     
  8.     # for filename in filelist:
  9.         # filepath=os.path.join(fpath,path+'/'+filename)
  10.         # if os.path.isdir(filepath):
  11.             # dirlist(filepath)
  12.         # print filepath
  13.     
  14.     
  15.     
  16.     
  17. # dirlist('a')


  18. import os
  19. g=os.walk('/root/server/testadu/a')
  20. for i in g:
  21.     print i

  22.     
  23.     
  24.     
  25. #
  26. for path,d,filelist in os.walk('/root/server/testadu/a'):
  27.     for filename in filelist:
  28.         os.path.join(path,filename)
  29. print path
异常处理

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #coding:utf-8
  3. print "Exception deal"
  4. #异常抛出机制 try except finally raise
  5. filename=raw_input('请输入你要操作的文件')
  6. try:
  7.     open(filename)
  8.     print hello
  9. except IOError,msg:
  10.     print "指定文件不存在"
  11. except NameError,msg:
  12.     print "内部变量调用有问题"
  13. print "over"
  14. finally:
  15.     print "ok"
  16. print "that's all"
  17. #抛出异常rasie
  18. if 'a'>5:
  19.     raise TypeError("Error:'a' must be iteger")







阅读(1312) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~