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()