最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python os模塊在系統(tǒng)管理中的應(yīng)用

 更新時(shí)間:2020年06月22日 14:46:28   作者:sea_kingdom  
這篇文章主要介紹了python os模塊在系統(tǒng)管理中的應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python os模塊在系統(tǒng)管理中的應(yīng)用代碼,供大家參考,具體內(nèi)容如下

#臨時(shí)文件

import tempfile 
tempfile.gettempdir()
#'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp'

tempfile.mkstemp()
#(4, 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\tmp9zc5ipzr')

tempfile.mkdtemp()
#'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\tmp94wxuh44'

#操作系統(tǒng)命令

import os
os.chdir(r'd:')
#切換到目錄(r為轉(zhuǎn)義字符)

os.listdir(r'd:')
#顯示目錄下的所有文件

os.makedirs(r'd:\1\1')
#創(chuàng)建路徑的所有文件

os.mkdir(r'd:\1')
#創(chuàng)建文件

#查找

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

#遍歷目錄

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

調(diào)用以下函數(shù)時(shí)要注意以下兩點(diǎn)

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

例如:解決第八個(gè)問題

start()
test8()

****d:\ptest、ptest下有三個(gè)目錄:document、files、temp。

import os,glob,shutil

def start():
 if os.path.exists(r'd:\ptest'):
 pass
 else:
 os.makedirs(r'd:\ptest\document')
 os.makedirs(r'd:\ptest\files')
 os.makedirs(r'd:\ptest\temp')

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

def test1():
 file_lists=glob.glob('c:\windows\*.ini')
 for file in file_lists:
 shutil.copy(file,r'd:\ptest\document')

(2)將c:\windows目錄下以'n'開頭的所有文件復(fù)制到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:\ptest\files')
  #temp.append(file)

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

def test3():
 if os.path.exists(r'd:\ptest\files\notepad.exe'):
 shutil.copy(r'd:\ptest\files\notepad.exe',r'd:\ptest\temp\mypad.exe')
 else:
 print("沒有notepad.exe文件")

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

def test4():
 if os.path.exists(r'd:\ptest\document\win.ini'):
 shutil.move(r'd:\ptest\document\win.ini',r'd:\ptest\temp')
 else:
 print("沒有win.ini文件")

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

def test5():
 if os.path.exists(r'd:\ptest\document\system.ini'):
 #復(fù)制刪除
 shutil.copy(r'd:\ptest\document\system.ini',r'd:\ptest\temp\system.inf')
 os.remove(r'd:\ptest\document\system.ini')
 
 #移動
 #shutil.move(r'd:\ptest\document\system.ini',r'd:\ptest\temp')
 else:
 print("沒有system.ini文件")

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

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

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

def test7():
 #移動
 shutil.move(r'd:\ptest\files',r'd:\ptest\document\mydir\myfiles')
 
 '''#復(fù)制,刪除
 file_lists=glob.glob(r'd:\ptest\files\*')
 print(file_lists)
 if os.path.exists(r'd:\ptest\document\mydir\myfiles'):
 pass
 else:
 os.mkdir(r'd:\ptest\document\mydir\myfiles')
 for file in file_lists:
 shutil.copy(file,r'd:\ptest\document\mydir\myfiles')
 os.remove(file)
 os.rmdir(r'd:\ptest\files')
 '''

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

def find(top,name):  #find與next_find形成一個(gè)輪回,只有發(fā)現(xiàn)文件,或文件夾為空時(shí)跳出
 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)
 #說明上述文件和目錄中無查找內(nèi)容,將目錄列表發(fā)給next_find函數(shù)
 next_find(dirnames,top,name)
 
def next_find(dirnames,top,name):
 for temp in dirnames:
  #目錄為空時(shí)跳出
  if not temp :
  break
  #更改遍歷目錄
  top=os.path.join(top,temp)
  #print(top)
  
  find(top,name)
import time
def test8():
 #將時(shí)間轉(zhuǎn)換為時(shí)間參數(shù)
 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')))
 #將時(shí)間參數(shù)轉(zhuǎn)換為標(biāo)準(zhǔn)時(shí)間
 print("最近訪問時(shí)間",time.strftime('%c',geta))
 print("最近修改時(shí)間",time.strftime('%c',getm))
 print("創(chuàng)建時(shí)間",time.strftime('%c',getc))
 print('大小%.3f'%(os.stat(find(r'd:\ptest','win.ini')).st_size/1024),'kB')

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實(shí)現(xiàn)自動化郵件發(fā)送過程詳解

    Python實(shí)現(xiàn)自動化郵件發(fā)送過程詳解

    這篇文章主要介紹了如何利用Python實(shí)現(xiàn)自動化郵件發(fā)送,可以讓你擺脫繁瑣的重復(fù)性業(yè)務(wù),可以節(jié)省非常多的時(shí)間。感興趣的小伙伴可以試一試
    2022-01-01
  • Python mlxtend庫數(shù)據(jù)科學(xué)和機(jī)器學(xué)習(xí)補(bǔ)充工具功能探索

    Python mlxtend庫數(shù)據(jù)科學(xué)和機(jī)器學(xué)習(xí)補(bǔ)充工具功能探索

    這篇文章主要介紹了Python mlxtend庫數(shù)據(jù)科學(xué)和機(jī)器學(xué)習(xí)補(bǔ)充工具功能探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Tensorflow實(shí)現(xiàn)在訓(xùn)練好的模型上進(jìn)行測試

    Tensorflow實(shí)現(xiàn)在訓(xùn)練好的模型上進(jìn)行測試

    今天小編就為大家分享一篇Tensorflow實(shí)現(xiàn)在訓(xùn)練好的模型上進(jìn)行測試,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python采用django框架實(shí)現(xiàn)支付寶即時(shí)到帳接口

    python采用django框架實(shí)現(xiàn)支付寶即時(shí)到帳接口

    這篇文章主要介紹了python采用django框架實(shí)現(xiàn)支付寶即時(shí)到帳接口的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • pytorch中的named_parameters()和parameters()

    pytorch中的named_parameters()和parameters()

    這篇文章主要介紹了pytorch中的named_parameters()和parameters()使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python Matplotlib 基于networkx畫關(guān)系網(wǎng)絡(luò)圖

    Python Matplotlib 基于networkx畫關(guān)系網(wǎng)絡(luò)圖

    這篇文章主要介紹了Python Matplotlib 基于networkx畫關(guān)系網(wǎng)絡(luò)圖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python小游戲?qū)崿F(xiàn)實(shí)例之接蘋果

    Python小游戲?qū)崿F(xiàn)實(shí)例之接蘋果

    其實(shí)利用Python編寫的小游戲很簡單,下面這篇文章主要給大家介紹了關(guān)于Python小游戲?qū)崿F(xiàn)實(shí)例之接蘋果的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • Python中如何實(shí)現(xiàn)真正的按位取反運(yùn)算

    Python中如何實(shí)現(xiàn)真正的按位取反運(yùn)算

    按位取反是位運(yùn)算符,而位運(yùn)算符是應(yīng)用在兩個(gè)數(shù)的運(yùn)算上,會對數(shù)字的二進(jìn)制所有位數(shù)進(jìn)行從低到高的運(yùn)算,下面這篇文章主要給大家介紹了關(guān)于Python中如何實(shí)現(xiàn)真正的按位取反運(yùn)算的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • Numpy數(shù)值積分的實(shí)現(xiàn)

    Numpy數(shù)值積分的實(shí)現(xiàn)

    本文主要介紹了Numpy數(shù)值積分的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • 詳解python中的Turtle函數(shù)庫

    詳解python中的Turtle函數(shù)庫

    這篇文章主要介紹了python中的Turtle函數(shù)庫,包括函數(shù)庫的引用方式,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11

最新評論

丁青县| 台江县| 长宁县| 云梦县| 哈密市| 贡觉县| 文水县| 南投县| 郴州市| 大宁县| 横山县| 通榆县| 万山特区| 惠州市| 海兴县| 宝应县| 康马县| 砀山县| 九龙坡区| 乐亭县| 太湖县| 兰溪市| 浏阳市| 阿拉善右旗| 南宫市| 安岳县| 扎囊县| 宿迁市| 三河市| 鄂州市| 乐陵市| 芮城县| 通河县| 中卫市| 靖江市| 萨迦县| 石景山区| 双鸭山市| 腾冲县| 醴陵市| 翁源县|