os模組使用教學,os模組教學

來源:互聯網
上載者:User

os模組使用教學,os模組教學
 # -*- coding:utf-8 -*-import osdef systemInfo():    #os.name 擷取系統的平台  Windows 返回 ‘nt'; Linux 返回’posix'    if os.name=='nt':        print "你的系統是Windows系統"    else:        print "你的系統不是Windows系統" #systemInfo()#-----------------------------------------------------------------------------------------#os.getcwd()——得到當前工作的目錄。def pwd():    print os.getcwd() #pwd()#-----------------------------------------------------------------------------------------   #os.listdir()——指定所有目錄下所有的檔案和目錄名def showFileName():    print os.listdir("c:\\") #showFileName() #-----------------------------------------------------------------------------------------  #os.remove()——刪除指定檔案def removeFile():    print os.remove("c:\\users\\administrator\\desktop\\runoob.txt") #removeFile()  #-----------------------------------------------------------------------------------------#os.rmdir()——刪除指定目錄def removeDir():    print os.removedirs("c:\\users\\administrator\\desktop\\testdir")#removeDir()  #-----------------------------------------------------------------------------------------#os.mkdir()——建立目錄 單層建立def createDir():    print os.mkdir("c:\\users\\administrator\\desktop\\demoDir1")#createDir()  #-----------------------------------------------------------------------------------------#os.makedirs()  遞迴建立目錄可建立多層def createDirs():    print os.makedirs("c:\\users\\administrator\\desktop\\demoDir2\\testFile\\111")#createDirs()   #-----------------------------------------------------------------------------------------#os.path.isfile()——判斷指定對象是否為檔案。是返回True,否則Falsedef isFiles(filename):    print os.path.isfile("c:\\users\\administrator\\desktop\\"+filename+"")#isFiles('cmd.exe')  非目錄返回True#isFiles('xxoo')   目錄返回False   #-----------------------------------------------------------------------------------------#os.path.isdir()——判斷指定對象是否為目錄。是True,否則Falsedef isdir():    print os.path.isdir("c:\\users\\administrator\\desktop\\xxoo")#isdir()  #-----------------------------------------------------------------------------------------#os.path.split()——返迴路徑的目錄和檔案名稱。def showPathSplit():    print os.path.split("c:\\users\\administrator\\desktop\\xxoo")#showPathSplit()   #-----------------------------------------------------------------------------------------#os.chdir()——改變目錄到指定目錄def changeDir():    print os.chdir("c:\\users\\administrator\\desktop")#changeDir()  #-----------------------------------------------------------------------------------------#os.path.getsize()——獲得檔案的大小,如果為目錄,返回0def getSize():    print os.path.getsize("c:\\users\\administrator\\desktop\\cmd.exe")#getSize()  #-----------------------------------------------------------------------------------------#os.path.abspath()——獲得絕對路徑def abspath():    print os.path.abspath("c:\\users\\administrator\\desktop\\")#abspath()   #-----------------------------------------------------------------------------------------#os.path.join(path, name)——串連目錄和檔案名稱def pathJoin():    print  os.path.join("c:\\users\\administrator\\desktop\\xxoo")#pathJoin()   #-----------------------------------------------------------------------------------------#os.path.basename(path)——返迴文件名def showbasename():    print os.path.basename("c:\\users\\administrator\\desktop\\cmd.exe")#showbasename()   #-----------------------------------------------------------------------------------------#os.path.dirname(path)——返迴文件路徑def showdirname():    print os.path.dirname("c:\\users\\administrator\\desktop\\cmd.exe")#showdirname()     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.