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
>>> 

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