python的file,time的一些知識點

1,open 和 with open的區別
open是手動打開,不需要的就需要關閉,不關閉可能會發現未知的錯誤

with open是自動打開,不需要的python會自動關閉

2,遍歷文本數據

files = open('python.txt','r',encoding='utf-8')

for line in files :
	print(line)
	
files.close()`

這樣相對來說沒有那麼佔用資源,比read 或者readlines一次性讀取 沒那麼佔用資源
但是 read 和readlines在整體可能處理速度要快,因爲for需要多次讀取數據

files = open('python.txt','r',encoding='utf-8')
content = files.readlines()

for line in content :
	print(line)

files.close()

3,seek 函數,指針偏移量
file.seek(0) 的作用是可以實現反覆讀取數據,而不需要反覆打開關閉文件 ,這是一個很好的技巧

4,os 中文件夾的操作
os.getcwd() #當前目錄
os.listdir() #打印當前文件夾內的文件
os.listdir(“F:\”) #打印指定文件夾內的文件

#判斷是否是一個文件

os.path.isfile("F\\pt.txt")

os.path.exists("F:\\")

os.rename("","")
吧目錄路徑和文件名分開
os.remove("F:\\tete.txt")
#創建文件夾
os.mkdir("F:\\package")
os.mkdirs("")

os.path.split("F:\\tt.txt")

# 查看文件名
# def basename(p):
#     """Returns the final component of a pathname"""
#     return split(p)[1]
# print(os.path.basename(path))  #注意 如果文件 爲 "D:/nihao/nihao" 那麼他輸出的是 nihao
# print(os.path.dirname(path))

以下代碼實現 創建指定文件中的目錄,如果包含文件,就創建文件,不包含就只創建目錄

# "D:/nihao/tmp.txt" 或者 實現 "D:/nihao" 類型的創建

path = "D:/nihao/tmp.txt"
tuple_1 = os.path.split(path)
print(tuple_1)

if "." in tuple_1[1]:
    os.makedirs(tuple_1[0])
    fo = open(path,"w")
    fo.close()
else:
    os.makedirs(path)

#文件的遍歷

os.walk()獲得三組數據(rootdir, dirname,filnames)

def file_path(file_dir):
    for root, dirs, files in os.walk(file_dir):
        print(root, end=' ')    # 當前目錄路徑,,注意 這裏的end = ' '表示print輸出的時候不換行
        print(dirs, end=' ')    # 當前路徑下的所有子目錄
        print(files)            # 當前目錄下的所有非目錄子文件

file_path("D:\PLSQL")

#獲取一個文件夾下面所有的文件的路徑

def find_file(filepath):
    files = os.listdir(filepath)
    for fi in files:
	# 在這裏join的目的是吧父路徑添加進去
        fi_d = os.path.join(filepath,fi)
        if os.path.isdir(fi_d):
            find_file(fi_d)
        else:
            print(os.path.join(filepath,fi_d))

find_file("D:\PLSQL\DataGenerator")

3 time模塊的用法
time.altzone 返回格林時間
time.asctime() 獲得時間元組 可以給他提供一個參數

time.clock() windows 和linux不一樣

常用
time.ctime() 獲取當前時間

常用
time.time() 獲取時間戳,從1970年到現在的秒數

time.gmtime() 返回一個時間元組 返回的事格林威治的時間元祖

常用
time.localtime()

#時間戳轉換成時間元祖,將時間元祖轉換成時間字符串

times = time.time()

tmp = time.localtime(times)

localtime返回一個時間元組:
索引 字段 值
0 4位數, 表示年份 2018,2019…
1 月份 1 ~ 12
2 日期 1 ~ 31
3 小時 0 ~ 23
4 分鐘 0 ~ 59
5 秒 0 ~ 61(60或61是閏秒)
6 星期幾 0 ~ 6(0是星期一)
7 一年的第幾天 1 ~ 366(朱利安日)
8 夏令時 -1,0,1,-1表示庫確定DST

print(time.strftime("%Y-%m-%d %H:%M:%S",tmp))

#吧時間字符串轉換成時間元祖
times = "2017-12-25 12:12:12"

tmp = time.strptime(times,'%Y-%m-%d %H:%M:%S')
print(tmp)

#把時間元祖轉換成時間戳
time.mktime(tmp)

time.sleep(5)#時間睡眠5秒

練習題目:獲得三天前的時間

threAgo = time.time() - 60*60*24*3
print(time.localtime(threAgo))

#---------------------------切換時間格式 --------------------------

“2017-11-24 17:30:00” 轉換成 “2017/11/24 17:30:00”

t = "2017-11-24 17:30:00"
#先轉換爲時間數組,然後轉換爲其他格式
timeStruct = time.strptime(t, "%Y-%m-%d %H:%M:%S")
strTime = time.strftime("%Y/%m/%d %H:%M:%S", timeStruct)
print(strTime)

----------------利用datetime來獲取時間

import datetime 
i = datetime.datetime.now() 
print ("當前的日期和時間是 %s" % i) 
print ("ISO格式的日期和時間是 %s" % i.isoformat() ) 
print ("當前的年份是 %s" %i.year) 
print ("當前的月份是 %s" %i.month) 
print ("當前的日期是 %s" %i.day) 
print ("dd/mm/yyyy 格式是  %s/%s/%s" % (i.day, i.month, i.year) ) 
print ("當前小時是 %s" %i.hour) 
print ("當前分鐘是 %s" %i.minute) 
print ("當前秒是  %s" %i.second)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章