py--目錄操作

目錄

 

1、使用前景

2、os模塊函數

3、os.walk練習題


1、使用前景

在將來的自動化測試中,經常會使用查找操作文件、查找測試報告、經常對大量的文件和大量的路徑進行操作,這就是依賴os模塊。

2、os模塊函數

  • os.getcwd():獲取當前目錄
  • os.curdir:返回當前目錄
  • os.pardir: 返回當前目錄的父目錄
  • os.chdir(path):返回到指定的目錄
  • os.name:獲取操作系統類型
  • os.mkdir(path):生成單級目錄
  • os.makedirs(path):生成多級目錄
  • os.removedirs(path):刪除多級爲空的目錄:若目錄爲空則刪除,遞歸到上級目錄,直到目錄不爲空。
  • os.rmdir(path):刪除單級非空目錄。
  • os.listdir(path):列出目錄內容
  • os.remove(filepath):刪除文件
  • os.rename(oldname,newname):重命名文件、目錄。
  • os.stat(path): 獲取文件的屬性:文件大小(st_size),文件訪問時間(st_atime),文件修改時間(st_mtime),文件創建時間(st_ctime)
  • os.system(command):執行系統命令
  • os.linesep:換行分隔符
  • os.pathsep:路徑分隔符
  • os.sep:操作系統分隔符
  • os.walk():目錄文件遍歷器
  • os.path.abspath(path):返回絕對路徑
  • os.path.split(path):分割路徑名和文件名:返回一個元組
  • os.path.splitext(path):分割文件名和擴展名:返回一個元組
  • os.path.dirname(path):返回目錄路徑,也就是os.path.split(path)的第一個元素
  • os.path.basename(path):返回文件名,也就是os.path.split(path)的第二個元素。
  • os.path.exists(path):判斷目錄\文件是否存在。
  • os.path.isabs(path):判斷是否爲絕對路徑。
  • os.path.isfile(path):判斷是否爲目錄。
  • os.path.join(a,*p):連接多個路徑名,中間以“\”分隔。
  • os.path.getatime(filename):返回最後訪問時間
  • os.path.getctime(filename):返回文件創建時間。
  • os.path.getmtime(filename):返回最後修改時間

3、os.walk練習題

前提:有一個文件夾位於e:\\testaa,該文件夾下有txt文件也有文件夾:test1、test2.二級文件夾下也有文件夾和txt文件。

練習題1:將文件夾testaa下的目錄以及文件夾都展示出來。

程序:

#coding=utf-8
import sys
sys.path.append("e:\\")

import os

for root,dirs,files in os.walk("e:\\testaa"):
    print("root:",root)
    print("dirs:",dirs)
    print("files::",files)
    print("*********************")

結果:

============================= RESTART: E:\aa.py =============================
root: e:\testaa
dirs: ['test1', 'test2']
files:: ['10001.txt', '20001.txt']
*********************
root: e:\testaa\test1
dirs: ['test3']
files:: ['30001.txt']
*********************
root: e:\testaa\test1\test3
dirs: []
files:: ['40001.txt']
*********************
root: e:\testaa\test2
dirs: ['test4']
files:: ['60001.txt']
*********************
root: e:\testaa\test2\test4
dirs: []
files:: ['70001.txt']
*********************
>>> 

練習題2:統計該文件夾下的目錄個數和文件個數。

程序:

#coding=utf-8
import sys
sys.path.append("e:\\")
import os
dir_count=0
file_count=0

for root,dirs,files in os.walk("e:\\testaa"):
    
    #print("dirs:",dirs)
    for i in dirs:
        dir_count+=1
    #print("files::",files)
    for k in files:
        file_count+=1

print("該文件夾下的目錄共有%d個"%dir_count)
print("該文件夾下的文件共有%d個"%file_count)

結果:

============================= RESTART: E:\aa.py =============================
該文件夾下的目錄共有4個
該文件夾下的文件共有6個
>>> 

練習題3:在上面的基礎上,將所有的文件夾和文件都生成絕對路徑。

#coding=utf-8

import os

dir_path=[]
file_path=[]

for root,dirs,files in os.walk("e:\\testaa"):
    #每一次循環都要取一下目錄中的當前路徑(root)、所有子目錄名字(dirs)、所有文件名字(files)
    
    print("root:",root)
    for dir in dirs:
        path=os.path.join(root,dir)
        dir_path.append(path)

    for file in files:
        path=os.path.join(root,file)
        file_path.append(path)


    print("*"*30)


print("子目錄的絕對路徑如下:",dir_path)
print("新目錄的絕對路徑如下:",file_path)

結果:

============================= RESTART: E:\aa.py =============================
root: e:\testaa
******************************
root: e:\testaa\test1hellohellohello
******************************
root: e:\testaa\test1hellohellohello\test3
******************************
root: e:\testaa\test2hellohellohello
******************************
root: e:\testaa\test2hellohellohello\test4
******************************
子目錄的絕對路徑如下: ['e:\\testaa\\test1hellohellohello', 'e:\\testaa\\test2hellohellohello', 'e:\\testaa\\test1hellohellohello\\test3', 'e:\\testaa\\test2hellohellohello\\test4']
新目錄的絕對路徑如下: ['e:\\testaa\\10001.txt', 'e:\\testaa\\20001.txt', 'e:\\testaa\\test1hellohellohello\\30001.txt', 'e:\\testaa\\test1hellohellohello\\test3\\40001.txt', 'e:\\testaa\\test2hellohellohello\\60001.txt', 'e:\\testaa\\test2hellohellohello\\test4\\70001.txt']
>>> 

練習題4、統計上面的例子中,一共有多少個txt文件。

#coding=utf-8

import os

dir_path=[]
file_path=[]
txt_count=0

for root,dirs,files in os.walk("e:\\testaa"):
   
    for file in files:
        if os.path.splitext(file)[1]==".txt":
            txt_count+=1
        
        
print("text文件一共有%d個"%txt_count)

結果:

============================= RESTART: E:\aa.py =============================
text文件一共有6個
>>> 

練習題5:將所有的文件名字後方都加上“aad”

#coding=utf-8

import os


for root,dirs,files in os.walk("e:\\testaa"):
    #將每個文件夾下的文件都取到
    for file in files:
        #取到文件的文件名,變化文件名稱,將“aad”加上
        new_front_path=os.path.splitext(file)[0]+"aad"
        #取到文件的後綴名
        new_behide_path=os.path.splitext(file)[1]
        #進行拼接
        new_name=new_front_path+new_behide_path
        #取新文件名的絕對路徑
        new_name_path=os.path.join(root,new_name)
        #取舊文件名的絕對路徑
        old_name_path=os.path.join(root,file)
        #替換名字
        os.rename(old_name_path,new_name_path)

        
    
for root,dirs,files in os.walk("e:\\testaa"):
    for file in files:
        print(os.path.join(root,file))

結果:

============================= RESTART: E:\aa.py =============================
e:\testaa\10001aadaad.txt
e:\testaa\20001aadaad.txt
e:\testaa\test2\60001aadaad.txt
e:\testaa\test2\test4\70001aadaad.txt
e:\testaa\test3\4322aadaad.txt
e:\testaa\test3\23we\2332aadaad.txt
>>> 

練習6、統計上述文件中如果是txt文件中的行數有多少,忽略空行。

#coding=utf-8

import os

count=0

for root,dirs,files in os.walk("e:\\testaa"):
    #將每個文件夾下的文件都取到
    for file in files:
        #判斷該文件是否爲txt文件,如果是則打開該文件
        if os.path.splitext(file)[1]==".txt":
            file=os.path.join(root,file)
            with open(file) as fp:
                for line in fp:
                    if line.strip()=="":
                        continue
                    count+=1

print("該文件夾下所有的txt文件中的行數(不包括空行)的數目爲:%d"%count)

結果:

============================= RESTART: E:\aa.py =============================
該文件夾下所有的txt文件中的行數(不包括空行)的數目爲:23
>>> 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章