博客笔记_python继续学习呀1127

	今天对python继续进行了学习,感觉python基础知识的学习也接近尾声了,后面学习完以后一定要以实际需求做个python的程序,一定要做出来个,加油,加油,加油!!!

u’’’
创建一个目录:年月日
进入这个目录,创建一个当前小时的目录
在进入小时的目录的,创建一个分钟的目录
再进入分钟的目录
创建一个文件,当前秒数为文件名,然后文件内容写上年月日时分秒
‘’’

import os

import time

def create_format_dir(file_path):

os.chdir(file_path)

dir1 = time.strftime("%Y-%m-%d")

os.mkdir(dir1)

os.chdir(dir1)

dir2 = time.strftime("%H")

os.mkdir(dir2)

os.chdir(dir2)

dir3 = time.strftime("%M")

os.mkdir(dir3)

os.chdir(dir3)

file4 = time.strftime("%S")

with open(os.getcwd()+os.sep+file4+".txt",‘w’) as fp:

fp.write(dir1+" “+dir2+”:"+dir3+":"+file4)

create_format_dir(“e:”)

import os

import time

def x(dir_name):

time_info = time.strftime("%Y-%m-%d")

if os.path.exists(dir_name) and os.path.isdir(dir_name):

os.chdir(dir_name)

os.mkdir(time_info)

os.chdir(time_info)

os.mkdir(time.strftime("%H"))

os.chdir(time.strftime("%H"))

os.mkdir(time.strftime("%M"))

os.mkdir(time.strftime("%M"))

with open(time.strftime("%S")+".txt",‘w’) as fp:

fp.write(time.strftime("%Y-%m-%d %H:%M:%S"))

x(“e:\test”)

import os

def count_lines_code(file_path):

lines=0

files=os.listdir(file_path)

#遍历路径下所有文件

for file in files:

#判断文件后缀为.py类型

if os.path.splitext(file)[1]==".py":

with open(file_path+file) as fp:

content=fp.read()

#以行为单位切割文件中内容

line_all=content.splitlines()

for line in line_all:

#如果为空行,则取消对该行的统计

if not line.strip():

line_all.remove(line)

#遍历统计每个文件的行数

lines+=len(line_all)

print u"上午代码py文件去掉空行后,总共行数为:",lines

#调用该文件统计方法

count_lines_code(“C:\Users\ThinkPadUser\Desktop\”)

#encoding = utf-8

print “1”

try:

print 1/0

except:

print “error,,报错了呀兄嘚”

# print 1/0

print “2”

try:

print “1”

print 1/0

print “2”

except:

print “error!”

while True:

try:

data = int(raw_input(“请输入一个数字:”.decode(‘utf-8’).encode(‘gbk’)))

data = int(data)

1/data

break

# except Exception,e:

# print e

# print u’请输入数字!’

except ZeroDivisionError,e:

print “ZeroDivisionError”,e

except ValueError,e:

print “ValueError”,e

except TypeError,e:

print “TypeError”,e

except Exception,e:

print e

print u"请输入数字!"

def create_file(file_path):

try:

with open(file_path,‘r’) as fp:

fp.read()

except Exception,e:

print e

with open(file_path,‘w+’) as fp:

fp.write(‘test\n’)

fp.write(‘yml’)

fp.seek(0,0)

fp.read()

print “it’s ok!”

create_file(“C:\Users\ThinkPadUser\Desktop\3.txt”)

import sys

try:

try:

print ‘a’ + 4

except ZeroDivisionError,e:

print “ZerpDivisionError”,e

except Exception,e:

print “Exception”,e

print “done”

def print_exception():

raise ValueError

try:

try:

print_exception()

except ZeroDivisionError,e:

print “ZeroDivisonError”,e

except ValueError,e:

print “ValueError1”,e

def print_exception():

return 1/0

try:

print_exception()

except:

print “exception1”

def print_exception():

return 1/0

try:

print_exception()

except(ValueError,IOError,ZeroDivisionError),e:

print “exception2”,e

import sys
try:
s = raw_input(‘Enter something -->’)
except KeyboardInterrupt:
print’\nWhy did you do an Ctrl + c on me?’
sys.exit()
except:
print ‘\nSome error/exception occurred.’
else:
print “no exception occur!”
finally:
print “finally is executed!”

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