天天看點

Python入門

作為一個javaer,腳本語言必須會幾個才行,友善自己。在這裡我就分享一下我的python入門學習經曆。還有一個小例子供參考。

一、環境搭建。

工欲善其事必先利其器,環境是必備的。在這裡我們就選擇最常用的eclipse+ pydev的方式。

下載下傳python,python2orpython3自己選擇。官網上的介紹很詳細。在這裡我們就用python2。

下載下傳pydev,可采用link的方式安裝,也可直接線上安裝。

eclipse設定

Python入門

二、執行個體學習

python的入門教程很多,喜歡step by step方式的同學可以上網搜尋學習。本人不太适應這種方式,是以就不贅述了。

解決問題是學習的動力,也是我比較喜歡的一種學習方式。通過自己的工具來解決工作中遇到的問題是一種享受。下面提供兩個小例子.

example1-緩存清理

         在部署經常需要清理緩存,是以我就想找一個工具來清理緩存,剛好又對python感興趣。寫了第一個python工具,很簡單。

import os

cachedir = 'c:\\oracle\\user_projects\\domains\\testprojects\\servers\\adminserver\\cache';

tmpdir = "c:\\oracle\\user_projects\\domains\\testprojects\\servers\\adminserver\\tmp";

logdir = "c:\\oracle\\user_projects\\domains\\testprojects\\servers\\adminserver\\logs";

os.system('rd /s /q ' + logdir);

os.system('rd /s /q ' + tmpdir);

os.system('rd /s /q ' + cachedir);

example2-資料統計

         在後來的工作中,又經常性遇到需要統計檔案行數和多個檔案總行數的要求。是以就又自己準備了一個小工具,幫助自己統計資料。

from dircache import listdir

def getfilelist(p):

    p = str(p)

    a = listdir(p)

    sumall = 0

    for x in a:

        filepath = p + "/" +x

        if os.path.isfile(filepath):

            linecount = len(open(filepath).readlines())

            sumall = sumall + linecount

            content = x + ":" + str(linecount)

            print content

    return sumall

print getfilelist("e:/compare")

三、結語

大緻就這麼多吧。python是非常強大的。大家可以一起學習交流,開發一些小工具。學以緻用。

Python入門

大小: 35.1 kb