色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

python os模塊在系統管理中的應用

瀏覽:27日期:2022-07-19 18:39:06

本文實例為大家分享了python os模塊在系統管理中的應用代碼,供大家參考,具體內容如下

#臨時文件

import tempfile tempfile.gettempdir()#’C:UsersADMINI~1AppDataLocalTemp’tempfile.mkstemp()#(4, ’C:UsersADMINI~1AppDataLocalTemptmp9zc5ipzr’)tempfile.mkdtemp()#’C:UsersADMINI~1AppDataLocalTemptmp94wxuh44’

#操作系統命令

import osos.chdir(r’d:’)#切換到目錄(r為轉義字符)os.listdir(r’d:’)#顯示目錄下的所有文件os.makedirs(r’d:11’)#創建路徑的所有文件os.mkdir(r’d:1’)#創建文件

#查找

import globglob.glob(’d:*.txt’)#目錄下的txt文件glob.glob(’d:*n.txt’)#目錄下的以n.txt結尾的文件

#遍歷目錄

import re,os,os.path def run(top): for(dirname,subdirs,files) in os.walk(top): print('['+dirname+']') for fname in files: print(os.path.join(dirname,fname))if __name__==’__main__’: run(r’d:1’)

調用以下函數時要注意以下兩點

(1)調用任何函數之前,要先調用start()函數。要有d:ptest、和ptest下有三個目錄:document、files、temp,才能進行其他操作(2)調用(1)-(8)函數,只需要test8()

例如:解決第八個問題

start()test8()

****d:ptest、ptest下有三個目錄:document、files、temp。

import os,glob,shutildef start(): if os.path.exists(r’d:ptest’): pass else: os.makedirs(r’d:ptestdocument’) os.makedirs(r’d:ptestfiles’) os.makedirs(r’d:ptesttemp’)

(1)將c:windows目錄下的所有ini文件復制到document中。

def test1(): file_lists=glob.glob(’c:windows*.ini’) for file in file_lists: shutil.copy(file,r’d:ptestdocument’)

(2)將c:windows目錄下以’n’開頭的所有文件復制到files中。

def test2(): file_lists=glob.glob(’c:windows*’) #temp=[]#以’n’開頭的所有文件 for file in file_lists: files=file.replace(’c:windows’,’’) if files.startswith(’n’): shutil.copy(file,r’d:ptestfiles’) #temp.append(file)

(3)判斷files文件夾中是否有notepad.exe文件,如果有,將其復制到temp中,并改名為mypad.exe。

def test3(): if os.path.exists(r’d:ptestfilesnotepad.exe’): shutil.copy(r’d:ptestfilesnotepad.exe’,r’d:ptesttempmypad.exe’) else: print('沒有notepad.exe文件')

(4)判斷document文件夾中是否有win.ini文件,如果有將其移動到temp中。

def test4(): if os.path.exists(r’d:ptestdocumentwin.ini’): shutil.move(r’d:ptestdocumentwin.ini’,r’d:ptesttemp’) else: print('沒有win.ini文件')

(5)判斷document文件夾中是否有system.ini文件,如果有將其以system.inf的名稱復制到temp中,然后刪除原文件。

def test5(): if os.path.exists(r’d:ptestdocumentsystem.ini’): #復制刪除 shutil.copy(r’d:ptestdocumentsystem.ini’,r’d:ptesttempsystem.inf’) os.remove(r’d:ptestdocumentsystem.ini’) #移動 #shutil.move(r’d:ptestdocumentsystem.ini’,r’d:ptesttemp’) else: print('沒有system.ini文件')

(6)在document下新建mydir文件夾,并將temp中的所有文件復制到mydir下。

def test6(): if os.path.exists(r’d:ptestdocumentmydir’): pass else: os.mkdir(r’d:ptestdocumentmydir’) ’’’#遍歷找出文件 for (dirpath,dirnames,filenames)in os.walk(r’d:ptestdocument’): for file in filenames: print(os.path.join(dirpath,file)) ’’’ file_lists=glob.glob(’d:ptestdocument*’) for file in file_lists: if os.path.isfile(file): if os.path.exists(file): print('文件已存在') else: shutil.copy(file,r’d:ptestdocumentmydir’)

(7)將files目錄及其內部所有文件以myfiles目錄名整體復制到mydir下,然后刪除原來的整個files目錄及其內部的所有文件。

def test7(): #移動 shutil.move(r’d:ptestfiles’,r’d:ptestdocumentmydirmyfiles’) ’’’#復制,刪除 file_lists=glob.glob(r’d:ptestfiles*’) print(file_lists) if os.path.exists(r’d:ptestdocumentmydirmyfiles’): pass else: os.mkdir(r’d:ptestdocumentmydirmyfiles’) for file in file_lists: shutil.copy(file,r’d:ptestdocumentmydirmyfiles’) os.remove(file) os.rmdir(r’d:ptestfiles’) ’’’

(8)找到此時notepad.exe文件的所在路徑,輸出其創建時間、最近訪問時間和最近修改時間,在輸出給文件的大小。

def find(top,name): #find與next_find形成一個輪回,只有發現文件,或文件夾為空時跳出 for (dirpath,dirnames,filenames) in os.walk(top): for file in filenames: if file==name: return os.path.join(dirpath,file) for dirs in dirnames: if dirs==name: return os.path.join(dirpath,dirs) #說明上述文件和目錄中無查找內容,將目錄列表發給next_find函數 next_find(dirnames,top,name) def next_find(dirnames,top,name): for temp in dirnames: #目錄為空時跳出 if not temp : break #更改遍歷目錄 top=os.path.join(top,temp) #print(top) find(top,name)import timedef test8(): #將時間轉換為時間參數 geta=time.gmtime(os.path.getatime(find(r’d:ptest’,’win.ini’))) getm=time.gmtime(os.path.getmtime(find(r’d:ptest’,’win.ini’))) getc=time.gmtime(os.path.getctime(find(r’d:ptest’,’win.ini’))) #將時間參數轉換為標準時間 print('最近訪問時間',time.strftime(’%c’,geta)) print('最近修改時間',time.strftime(’%c’,getm)) print('創建時間',time.strftime(’%c’,getc)) print(’大小%.3f’%(os.stat(find(r’d:ptest’,’win.ini’)).st_size/1024),’kB’)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 泷泽萝拉亚洲精品中文字幕 | 91精品免费国产高清在线 | 国产在线观看一区二区三区四区 | 小明台湾成人永久免费看看 | 亚洲欧美日韩在线线精品 | 欧美一级特黄视频 | 青青草国产免费一区二区 | 国产99视频精品草莓免视看 | 国产免费资源 | 性色午夜视频免费男人的天堂 | 91天仙tv嫩模福利 | 国产色司机在线视频免费观看 | 亚洲第一成年免费网站 | 欧美性69| 好吊妞国产欧美日韩视频 | 97视频免费观看 | 国产第一页久久亚洲欧美国产 | 国内精品久久久久久影院老狼 | 欧美zoofilia杂交videos | 最新精品亚洲成a人在线观看 | 红色记忆 | 特级毛片免费观看视频 | 亚洲三级在线看 | 国产一级毛片夜一级毛片 | 成年免费观看 | 国产午夜精品一区二区三区不卡 | 国产真人毛片一级视频 | 日朝欧美亚洲精品 | 久久精品全国免费观看国产 | 99久久国产免费中文无字幕 | 久久semm亚洲国产 | 97视频在线看 | 国产精品成人观看视频国产 | 国产在线精品一区二区不卡 | 日本一本色道 | 欧美三级免费网站 | 手机毛片在线观看 | 国产一级做a爰片在线看免费 | 美女视频免费黄色 | 成人在线免费观看视频 | 精品一区二区三区在线视频 |