一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python文件及目錄操作實例詳解

Python文件及目錄操作實例詳解

2020-07-12 11:33raygtr Python

這篇文章主要介紹了Python文件及目錄操作的方法,實例分析了Python中os模塊的使用技巧,需要的朋友可以參考下

本文實例講述了Python文件目錄操作的方法。分享給大家供大家參考。具體分析如下:

在python中對文件及目錄的操作一般涉及多os模塊,os.path模塊。具體函數以及使用方法在程序中說明。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
#-*- coding=UTF8 -*-
import os
import os.path as op
def change_dir():
  '''
 該函數顯示及改變前目錄
 using chdir() to change current dir
    getcwd() can show the current working directory
  '''
  directory="/tmp"
  #使用getcwd()返回當前目錄
  print os.getcwd()
  #chdir改變當前目錄為:directory目錄
  os.chdir(directory)
  print os.getcwd()
def show_filesOfdir(whichDir):
  '''
 此函數只顯示目錄下的所有文件
 using listdir() to shows all of the file execpt directory
   join() function catenate 'whichDir' with listdir() returns values
   isfile() check that file is a regular file
   '''
   #listdir() 函數顯示前目錄的內容
  for file in os.listdir(whichDir):
 #利用join()把whichDir目錄及listdir() 返回值連接起來組成合法路徑
    file_name = op.join(whichDir,file)
 #isfile()函數可以判斷該路徑上的文件是否為一個普通文件
    if op.isfile(file_name):
      print file_name
def printaccess(path):
  '''
 顯示文件的最后訪問時間,修改時間
 shows 'path' the last access time
      getatime() return the time of last access of path
   stat() return information of a file,use its st_atime return the time of last access
   ctime() return a string of local time
  '''
  import time
  #利用ctime()函數返回最后訪問時間
  #getatime()函數返回最后訪問時間,不過是以秒為單位(從新紀元起計算)
  print time.ctime(op.getatime(path))
  #stat()函數返回一個對象包含文件的信息
  stat = os.stat(path)
  #st_atime 最后一次訪問的時間
  print time.ctime(stat.st_atime)
  print the modify time
  print "modify time is:",
  print time.ctime(op.getctime(path))
  print "modify time is:",
  #st_ctime 最后一次修改的時間
  print time.ctime(stat.st_ctime)
def isDIR(path):
  '''
 一個os.path.isdir()函數的實現
 Implement isdir() function by myself
  '''
  import stat
  MODE = os.stat(path).st_mode
  #返回真假值
  return stat.S_ISDIR(MODE)
if __name__== "__main__":
  change_dir()
  show_filesOfdir('''/root''')
  printaccess('/etc/passwd')
  print isDIR('/etc')

希望本文所述對大家的Python程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久99re热在线播放7 | 国产成人精品免费视频大全五级 | 91热国产 | 免费高清www动漫视频播放器 | 亚洲AV久久无码精品九号 | 欧美亚洲韩国 | 日韩精品视频在线播放 | 亚洲视频在线观看不卡 | 含羞草传媒一天免费看下 | 我的奶头被客人吸的又肿又红 | 天莱男模gary | 艹b小说 | 精品一区二区三区 不卡高清 | 亚洲国产天堂在线观看 | 免费视频大全 | hd在线观看免费高清视频 | 免费一区 | 2019午夜福合集高清完整版 | 国产日韩欧美色视频色在线观看 | 日韩成人免费aa在线看 | 草莓影音 | 香蕉国产人午夜视频在线 | 国内精品91久久久久 | 欧美日韩亚洲高清不卡一区二区三区 | 俄罗斯图书馆无打码久久 | 日韩r | 婷婷综合在线 | 人皮高跟鞋在线观看 | 亚洲精品人成网在线播放影院 | 久久精品国产色蜜蜜麻豆国语版 | 秀逼逼| 日本www午夜色在线视频 | 国产自拍偷拍自拍 | 日本暖暖在线视频 | 日韩美毛片 | 香蕉免费一区二区三区在线观看 | 国产精品成人扳一级aa毛片 | 日本草草视频 | 奇米影视4444| 亚洲欧美一区二区久久 | 免费视频片在线观看大片 |