Python文件時間操作步驟代碼詳解
一 按時間創(chuàng)建文件
源碼
# 截圖方式二
# coding=utf-8
import os
import time
# 當前年月日時分秒時間 2020-01-16-10_11_49
picture_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
#當前年月日 2020-01-16
directory_time = time.strftime("%Y-%m-%d", time.localtime(time.time()))
print('當前年月日時分秒時間:'+ picture_time)
print("當前年月日:"+directory_time)
# 獲取當前文件目錄
print('當前文件目錄:'+os.getcwd())
# 獲取到當前文件的目錄,并檢查是否有 directory_time 文件夾,如果不存在則自動新建 directory_time 文件
try:
File_Path = os.getcwd() + '\\' + directory_time + '\\'
print(os.path)
#exists判斷文件路徑是否存在
if not os.path.exists(File_Path):
os.makedirs(File_Path)
print("目錄新建成功:%s" % File_Path)
else:
print("目錄已存在?。。?)
except BaseException as msg:
print("新建目錄失?。?s" % msg)
#切換目錄
os.chdir("D:/git")
print('切換后的目錄位置:'+os.getcwd())
源碼執(zhí)行控制臺打?。?/p>
當前年月日時分秒時間:2020-01-16-11_19_12
當前年月日:2020-01-16
當前文件目錄:D:\git\gongcheng
<module 'ntpath' from 'D:\Python36\lib\ntpath.py'>
目錄新建成功:D:\git\gongcheng\2020-01-16
切換后的目錄位置:D:\git
二 獲取環(huán)境變量、進程、父進程
源代碼
import os
#獲取系統(tǒng)環(huán)境變量
print("環(huán)境變量是:"+os.environ["CLASSPATH"])
#獲取當前進程ID
print(os.getpid())
#獲取父進程ID
print(os.getppid())
源碼執(zhí)行控制臺打印:
環(huán)境變量是:.;C:\Program Files\Java\jdk1.8.0_101\lib\dt.jar;C:\Program >Files\Java\jdk1.8.0_101\lib\tools.jar;
10760
11224
三、獲取當前文件的創(chuàng)建、修改、訪問時間
源碼
import time
import os
filepath = 'D:\gongcheng'
#獲取文件的創(chuàng)建時間 get create time
ctime = os.path.getctime(filepath)
print("創(chuàng)建時間是:"+time.ctime(ctime))
#獲取文件的修改時間 get modify time
utime = os.path.getmtime(filepath)
print("修改時間是:"+time.ctime(utime))
#獲取文件的訪問時間 get active time
atime = os.path.getatime(filepath)
print("訪問時間是:"+time.ctime(atime))
源碼執(zhí)行控制臺打印:
創(chuàng)建時間是:Fri Jul 5 19:13:27 2019
修改時間是:Mon Jan 13 18:27:26 2020
訪問時間是:Mon Jan 13 18:27:26 2020
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- python 代碼運行時間獲取方式詳解
- python獲取本周、上周、本月、上月及本季的時間代碼實例
- Python sqlalchemy時間戳及密碼管理實現代碼詳解
- Python代碼執(zhí)行時間測量模塊timeit用法解析
- Python實現進度條和時間預估的示例代碼
- 利用4行Python代碼監(jiān)測每一行程序的運行時間和空間消耗
- python 實現仿微信聊天時間格式化顯示的代碼
- Python計算公交發(fā)車時間的完整代碼
- Python實現bilibili時間長度查詢的示例代碼
- Python統(tǒng)計時間內的并發(fā)數代碼實例
- 如何基于python測量代碼運行時間
- python 統(tǒng)計代碼耗時的幾種方法分享
相關文章
Python 利用Entrez庫篩選下載PubMed文獻摘要的示例
這篇文章主要介紹了Python 利用Entrez庫篩選下載PubMed文獻摘要的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11
Django uwsgi Nginx 的生產環(huán)境部署詳解
這篇文章主要介紹了Django uwsgi Nginx 的生產環(huán)境部署詳解,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
python開發(fā)實例之Python的Twisted框架中Deferred對象的詳細用法與實例
這篇文章主要介紹了python開發(fā)實例之Python的Twisted框架中Deferred對象的詳細用法與實例,需要的朋友可以參考下2020-03-03

