今天對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!”