Python 資料流操作

來源:互聯網
上載者:User

Python 檔案或檔案夾操作

shutil該模組提供了很多進階的多檔案和多檔案集合的操作,尤其提供了支援檔案複製和刪除的函數。
1、檔案夾和檔案操作

import shutil shutil.copyfile(src, dst)    複製檔案src的內容到檔案dst,dst必須是完整的目標檔案名,如果dst已經存在,它將會被替換 shutil.copy(src, dst)    複製檔案src到檔案或檔案夾dst。如果dst是一個檔案夾,與src相同名字的檔案在dst下被建立或重寫。 shutil.copytree(src, dst, symlinks=False, ignore=None)  遞迴的複製src下的整個目錄到檔案夾dst,dst不能是已經存在的,如果不存在,它將會被建立。 shutil.rmtree(path)  刪除path整個分類樹內容 os.rmdir(path)、  刪除檔案夾path,path必須是空檔案夾,否則報OSError錯誤 shutil.move(src, dst)  遞迴的移動src到另一個位置dst,src可以是一個檔案或一個檔案夾

2,遍曆檔案夾下每個檔案

import osnames = os.listdir(src)for name in names:    srcname = os.path.join(src, name)    fo = open(srcname, 'r')    for line in fo:        print line

3,判斷檔案夾是否存在,不存在則建立

if not os.path.exists(FILE_PATH):        os.makedirs(FILE_PATH)

Python 串連MySQL及基本操作

def get_conn():    import MySQLdb    try:        conn=MySQLdb.connect(host=HOST,user=USER,passwd=PASSWORD, port=PORT, db=DBNAME, charset=CHARSET)        return conn    except MySQLdb.Error,e:        print "Mysql Error %d: %s" % (e.args[0], e.args[1])# 插入操作def insert_one(cur,sql,value):    res =  cur.execute(sql ,value)    # 插入成功,res 傳回值為1    if  1 != res :        print 'failed'    else:        print 'success'def insert_many(cur,sql,values):    res =  cur.executemany(sql ,values)    # 插入成功,res 傳回值為1    if  1 != res :        print 'failed'    else:        print 'success'getRC = lambda cur: cur.rowcount if hasattr(cur,'rowcount')  else -1# 更新操作def update(cur,sql):    cur.execute(sql)    return getRC(cur)# 刪除操作def delete(cur,sql):    cur.execute(sql)    return getRC(cur)# 只擷取一條記錄,返回的是一個元組def fetch_one(cur,sql):    count = cur.execute(sql)    result = cur.fetchone()    return result# 擷取多條資料;返回的是二維元組def fetch_all(cur,sql):    count = cur.execute(sql)    results = cur.fetchall()    return results# 提交的完成操作def finish(conn):    conn.commit()    conn.close()

Python 檔案或檔案夾操作

shutil該模組提供了很多進階的多檔案和多檔案集合的操作,尤其提供了支援檔案複製和刪除的函數。
1、檔案夾和檔案操作

import shutil shutil.copyfile(src, dst)    複製檔案src的內容到檔案dst,dst必須是完整的目標檔案名,如果dst已經存在,它將會被替換 shutil.copy(src, dst)    複製檔案src到檔案或檔案夾dst。如果dst是一個檔案夾,與src相同名字的檔案在dst下被建立或重寫。 shutil.copytree(src, dst, symlinks=False, ignore=None)  遞迴的複製src下的整個目錄到檔案夾dst,dst不能是已經存在的,如果不存在,它將會被建立。 shutil.rmtree(path)  刪除path整個分類樹內容 os.rmdir(path)、  刪除檔案夾path,path必須是空檔案夾,否則報OSError錯誤 shutil.move(src, dst)  遞迴的移動src到另一個位置dst,src可以是一個檔案或一個檔案夾

2,遍曆檔案夾下每個檔案

import osnames = os.listdir(src)for name in names:    srcname = os.path.join(src, name)    fo = open(srcname, 'r')    for line in fo:        print line

3,判斷檔案夾是否存在,不存在則建立

if not os.path.exists(FILE_PATH):        os.makedirs(FILE_PATH)

Python 串連MySQL及基本操作

def get_conn():    import MySQLdb    try:        conn=MySQLdb.connect(host=HOST,user=USER,passwd=PASSWORD, port=PORT, db=DBNAME, charset=CHARSET)                return conn    except MySQLdb.Error,e:                print "Mysql Error %d: %s" % (e.args[0], e.args[1])# 插入操作def insert_one(cur,sql,value):    res =  cur.execute(sql ,value)    # 插入成功,res 傳回值為1    if  1 != res :        print 'failed'    else:        print 'success'def insert_many(cur,sql,values):    res =  cur.executemany(sql ,values)    # 插入成功,res 傳回值為1    if  1 != res :        print 'failed'    else:        print 'success'getRC = lambda cur: cur.rowcount if hasattr(cur,'rowcount')      else -1# 更新操作def update(cur,sql):    cur.execute(sql)    return getRC(cur)# 刪除操作def delete(cur,sql):    cur.execute(sql)    return getRC(cur)# 只擷取一條記錄,返回的是一個元組def fetch_one(cur,sql):    count = cur.execute(sql)    result = cur.fetchone()    return result# 擷取多條資料;返回的是二維元組def fetch_all(cur,sql):    count = cur.execute(sql)    results = cur.fetchall()    return results# 提交的完成操作def finish(conn):    conn.commit()    conn.close()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.