python學習筆記2

2021-09-06 03:38:51 字數 1514 閱讀 9705

建立乙個檔案並往裡面寫入內容

import os

# coding='utf-8'

#取得os的換行符

ls = os.linesep

# 獲取檔名

while true:

fname = raw_input('input file name: ')

if os.path.exists(fname):

print "error : '%s' already exists!" % fname

else:

break

print fname

#獲取檔案的內容

all =

print "\nenter lines('.' by iteslf to quit).\n"

#讀取使用者的輸入

while true:

entry = raw_input('> ')

if entry == '.':

break

else:

#寫入檔案

fobj = open(fname,'w')

fobj.writelines(['%s%s' % (x, ls ) for x in all])

fobj.close()

print "done"

讀取檔案內容並顯示出來

# coding='utf-8'

# 獲取檔名

fname = raw_input('input file name: ')

print fname

try:

fobj = open(fname,'r')

except ioerror, e:

print "**** file open: ",e

else:

for eachline in fobj:

print eachline,

fobj.close();

獲取數值的型別.注意python作為高階語言,卻沒有對數值進行統一,還是分屬於幾個型別:整型,浮點型,長整型,複數

# coding='utf-8'

# 獲取數值的型別

def getnumtype(num):

print num, 'is',

#判定引數一是否為引數二當中的某個元素的例項

if isinstance(num, (int,long,float,complex)):

print 'a number of type: ',type(num).__name__

else:

print 'not a number at all'

getnumtype(-68)

getnumtype(99999999999999999999999999999l)

getnumtype(98.6)

getnumtype(-5.2+1.9j)

getnumtype("1212")

Python學習筆記 2

python學習筆記 2 1 error and exceptions 錯誤和異常 語法錯誤是在編譯時檢查,但python允許在程式執行期間檢查錯誤。當檢查出錯誤,python直譯器丟擲 產生 觸發乙個異常。要增加錯誤檢測或異常處理到 使用try except語句。語法如下 try try runn...

python學習筆記 2

八 type函式的作用是顯示值和變數的型別,id以值或變數為引數,返回值是一整數.type world type str id 123 11602164 九 python函式的定義形式 def arg1,arg2,argn 函式的名字也必須以字母開頭,可以包括下劃線 但不能把python的 關鍵字定...

Python學習筆記 2

已經學習python兩天了,總結一下學習成果。初學python,如果有寫錯的地方,還望各位大牛指出。先列一列作品 python socket 客戶端測試程式 easygui 猜數遊戲 看雪登入器 涉及到的知識點 物件導向 socket庫 httplib2庫 easygui 小知識點 通過這種方式匯入...