激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

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

服務(wù)器之家 - 腳本之家 - Python - Python文件及目錄操作實(shí)例詳解

Python文件及目錄操作實(shí)例詳解

2020-07-12 11:33raygtr Python

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

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

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

?
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():
  '''
 該函數(shù)顯示及改變前目錄
 using chdir() to change current dir
    getcwd() can show the current working directory
  '''
  directory="/tmp"
  #使用getcwd()返回當(dāng)前目錄
  print os.getcwd()
  #chdir改變當(dāng)前目錄為:directory目錄
  os.chdir(directory)
  print os.getcwd()
def show_filesOfdir(whichDir):
  '''
 此函數(shù)只顯示目錄下的所有文件
 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() 函數(shù)顯示前目錄的內(nèi)容
  for file in os.listdir(whichDir):
 #利用join()把whichDir目錄及l(fā)istdir() 返回值連接起來組成合法路徑
    file_name = op.join(whichDir,file)
 #isfile()函數(shù)可以判斷該路徑上的文件是否為一個(gè)普通文件
    if op.isfile(file_name):
      print file_name
def printaccess(path):
  '''
 顯示文件的最后訪問時(shí)間,修改時(shí)間
 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()函數(shù)返回最后訪問時(shí)間
  #getatime()函數(shù)返回最后訪問時(shí)間,不過是以秒為單位(從新紀(jì)元起計(jì)算)
  print time.ctime(op.getatime(path))
  #stat()函數(shù)返回一個(gè)對(duì)象包含文件的信息
  stat = os.stat(path)
  #st_atime 最后一次訪問的時(shí)間
  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 最后一次修改的時(shí)間
  print time.ctime(stat.st_ctime)
def isDIR(path):
  '''
 一個(gè)os.path.isdir()函數(shù)的實(shí)現(xiàn)
 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')

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美精品久久久久久久久老牛影院 | 午夜视频在线观看91 | 亚洲精品在线观看网站 | 国产视频软件在线 | 夜夜看 | 一区二区三区视频播放 | 日本中文字幕久久 | 色综合久久久久久久久久 | 黄网站在线免费 | 成年男女免费视频 | 精品国产一区二区三区四区阿崩 | 国产69精品99久久久久久宅男 | 一级大片一级一大片 | 毛片大全免费 | 日本一区二区三区视频在线 | 欧美在线成人影院 | 在线中文字幕观看 | 羞羞视频免费观看网站 | 欧美不卡 | 国产精品久久久久久238 | 请播放一级毛片 | 午夜视频在线在免费 | 成人午夜看片 | 渔夫荒淫艳史 | 欧美日韩一 | 久久精品国产清自在天天线 | 538任你躁在线精品视频网站 | 久国久产久精永久网页 | 91av在线免费观看 | 国产精品免费久久久久久 | 国产一区精品视频 | 国产精品一品二区三区四区18 | 欧美一级做一级爱a做片性 毛片电影网址 | 精品视频一区二区三区四区 | 欧美成人精品不卡视频在线观看 | 久久久久久69 | 在线无码| 久久激情免费视频 | 91免费高清视频 | 黄视频免费在线观看 | 蜜桃麻豆视频 |