S_ISCHR等

來源:互聯網
上載者:User

import os,time,stat

fileStats = os.stat ( 'test.txt' )                        #擷取檔案/目錄的狀態
fileInfo = {
'Size':fileStats [ stat.ST_SIZE ],                        #擷取檔案大小
'LastModified':time.ctime( fileStats [ stat.ST_MTIME ] ), 
#擷取檔案最後修改時間
'LastAccessed':time.ctime( fileStats [ stat.ST_ATIME ] ), 
#擷取檔案最後訪問時間
'CreationTime':time.ctime( fileStats [ stat.ST_CTIME ] ), 
#擷取檔案建立時間
'Mode':fileStats [ stat.ST_MODE ]                         #擷取檔案的模式
}
#print fileInfo

for field in fileInfo:                                    #顯示對象內容
  print '%s:%s' % (field,fileInfo[field])

for infoField,infoValue in fileInfo:
  print '%s:%s' % (infoField,infoValue)
if stat.S_ISDIR ( fileStats [ stat.ST_MODE ] ):           #判斷是否路徑
  print 'Directory. '
else:
  print 'Non-directory.'

if stat.S_ISREG( fileStats [ stat.ST_MODE ] ):          #判斷是否一般檔案
   print 'Regular file.'
elif stat.S_ISLNK ( fileStats [ stat.ST_MODE ] ):        #判斷是否連結檔案
   print 'Shortcut.'
elif stat.S_ISSOCK ( fileStats [ stat.ST_MODE ] ):       #判斷是否通訊端檔案    
   print 'Socket.'
elif stat.S_ISFIFO ( fileStats [ stat.ST_MODE ] ):       #判斷是否具名管道
   print 'Named pipe.'
elif stat.S_ISBLK ( fileStats [ stat.ST_MODE ] ):        #判斷是否塊裝置
   print 'Block special device.'
elif stat.S_ISCHR ( fileStats [ stat.ST_MODE ] ):        #判斷是否字元設定
   print 'Character special device.'

   stat模組描述了os.stat(filename)返回的檔案屬性列表中各值的意義.我們可方便地根據stat模組存取os.stat()中的值.
os.stat(path)執行一個stat()系統調用在給定的path上,返回一個類元組對象(stat_result對象,包含10個元素),屬性與stat結構成員相關:st_mode(許可權模式),st_ino(inode number),st_dev(device),st_nlink(number
of hard links),st_uid(所有使用者的user id),st_gid(所有使用者的group id),st_size(檔案大小,以位為單位),st_atime(最近訪問的時間),st_mtime(最近修改的時間),st_ctime(建立的時間)

>>> import os
>>> print os.stat("/root/python/zip.py")
(33188, 2033080, 26626L, 1, 0, 0, 864, 1297653596, 1275528102, 1292892895)
>>> print os.stat("/root/python/zip.py").st_mode  
#許可權模式
33188
>>> print os.stat("/root/python/zip.py").st_ino   #inode number
2033080
>>> print os.stat("/root/python/zip.py").st_dev   
#device
26626
>>> print os.stat("/root/python/zip.py").st_nlink 
#number of hard links
1
>>> print os.stat("/root/python/zip.py").st_uid   
#所有使用者的user id
0
>>> print os.stat("/root/python/zip.py").st_gid   
#所有使用者的group id
0
>>> print os.stat("/root/python/zip.py").st_size  #檔案的大小,以位為單位
864
>>> print os.stat("/root/python/zip.py").st_atime 
#檔案最後訪問時間
1297653596
>>> print os.stat("/root/python/zip.py").st_mtime 
#檔案最後修改時間
1275528102
>>> print os.stat("/root/python/zip.py").st_ctime 
#檔案建立時間
1292892895
[Overview][Constants][Types][Procedures and functions][Variables][Index] Reference for unit 'oldlinux' (#rtl)
S_ISCHR

Is file a character device

Declaration

Source position: oldlinux.pp line 1603

function S_ISCHR(

  m: Word

):Boolean;

Description

S_ISCHR checks the file mode m to see whether the file is a character device file. If so it returns
True.

See also

FStat

  

Retrieve information about a file

S_ISLNK

  

Is file a symbolic link

S_ISREG

  

Is file a regular file

S_ISDIR

  

Is file a directory

S_ISBLK

  

Is file a block device

S_ISFIFO

  

Is file a FIFO

S_ISSOCK

  

Is file a unix socket

聯繫我們

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