Python(os模組)

來源:互聯網
上載者:User

標籤:指令碼   lex   查看   moved   指定   finish   shel   pychar   stat   

os模組

 1 import os 2 import time 3 print(os.getcwd()) #py執行檔案路徑,和在console下不一樣 4  5 os.chdir(r‘/home/nizhipeng‘) #改變當前指令碼目錄;相當於cd,工作環境發生改變 6 print(os.getcwd()) 7  8 print(os.curdir) #.返回目前的目錄: (‘.‘) 9 print(os.pardir) #..擷取目前的目錄的父目錄字元串名:(‘..‘)10 11 #在nizhipeng檔案下查看變化12 os.makedirs(‘abc/alex‘)#當前工作目錄已改變,為/home/nizhipeng,所以在其路徑下產生了多層遞迴目錄 abc/alex13 time.sleep(.5)14 os.removedirs(‘abc/alex‘)#只刪除空檔案夾15 16 #在nizhipeng檔案下查看變化17 os.mkdir(‘efd‘)    #產生單級目錄;相當於shell中mkdir dirname18 time.sleep(.5)19 os.rmdir(‘efd‘)    #刪除單級空目錄,若目錄不為空白則無法刪除,報錯;相當於shell中rmdir dirname20 21 dirs = os.listdir(r‘/home/nizhipeng/PycharmProjects/learnPython/week4‘)22 print(dirs) #列出指定目錄下的所有檔案和子目錄,包括隱藏檔案,並以列表方式列印23 24 #os.remove(‘ppt.py‘)# 在/home/nizhipeng 工作空間下,只能刪檔案,不能刪檔案夾25 26 #os.rename("ww","www") #重新命名檔案/目錄 在/home/nizhipeng 工作空間下27 28 info = os.stat(‘./www‘)29 print(info)30 31 print(os.sep) #輸出作業系統特定的路徑分隔字元 / ,在linux下為/,但是在win下為\\,跨平台時會涉及到這種情況

執行結果:

/home/nizhipeng/PycharmProjects/learnPython/week4/day5/home/nizhipeng...[‘day4‘, ‘day5‘]os.stat_result(st_mode=16893, st_ino=1079409, st_dev=2056, st_nlink=2, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1535548162, st_mtime=1535548153, st_ctime=1535548162)/Process finished with exit code 0

 

參考:http://www.cnblogs.com/alex3714/articles/5161349.html

Python(os模組)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.