第4章 4.2 遍历和搜索目录

一、主要方法为os.walk():

(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ mkdir dir
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ touch dir/file1.txt
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ touch dir/file2.txt
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ mkdir dir/subdir
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ touch dir/subdir/file3.txt
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ touch dir/subdir/file4.txt
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ touch dir/subdir/file5.pdf
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ touch dir/file6.pdf
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ cd dir/
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04/dir$ python
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> for root, dirs, files in os.walk('.'):
... for file in files:
... print(file)
...
file6.pdf
file2.txt
file1.txt
file4.txt
file5.pdf
file3.txt
>>>

 

>>> for root, dirs, files in os.walk('.'):
... for file in files:
... full_file_path = os.path.join(root, file)
... print(full_file_path)
...
./file6.pdf
./file2.txt
./file1.txt
./subdir/file4.txt
./subdir/file5.pdf
./subdir/file3.txt
>>>
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04/dir$ cd ..
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ python
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> for root, dirs, files in os.walk('.'):
... for file in files:
... full_file_path = os.path.join(root, file) #将脚本所有目录与文件组成完整路径
... print(full_file_path)
...
./dir/file6.pdf
./dir/file2.txt
./dir/file1.txt
./dir/subdir/file4.txt
./dir/subdir/file5.pdf
./dir/subdir/file3.txt

 

3. 只打印.pdf文件:

>>> for root, dirs, files in os.walk('.'):
... for file in files:
... if file.endswith('.pdf'):
... full_file_path = os.path.join(root, file)
... print(full_file_path)
...
./dir/file6.pdf
./dir/subdir/file5.pdf

 

4. 只打印文件名包含偶数的文件:

>>> import re
>>> for root, dirs, files in os.walk('.'):
... for file in files:
... if re.search(r'[13579]', file): #取出
... full_file_path = os.path.join(root, file)
... print(full_file_path)
...
./dir/file1.txt
./dir/subdir/file5.pdf
./dir/subdir/file3.txt

 

>>> for root, dirs, files in os.walk('.'): #root为主目录字符串, dirs为子目录列表, files返回文件列表
... print(root, dirs, files)
...
. ['dir'] []
./dir ['subdir'] ['file6.pdf', 'file2.txt', 'file1.txt']
./dir/subdir [] ['file4.txt', 'file5.pdf', 'file3.txt']

 

除此之外:

>>> for root, dirs, files in os.walk('.'):
... for file in files:
... print(os.path.abspath(file))
...
/home/metal/project/PAutomationCookbook/ch04/file6.pdf
/home/metal/project/PAutomationCookbook/ch04/file2.txt
/home/metal/project/PAutomationCookbook/ch04/file1.txt
/home/metal/project/PAutomationCookbook/ch04/file4.txt
/home/metal/project/PAutomationCookbook/ch04/file5.pdf
/home/metal/project/PAutomationCookbook/ch04/file3.txt

#os.path.abspath(file)返回文件的绝对路径

 

>>> os.path.split('/home/metal/project/PAutomationCookbook/ch04/file1.txt')
('/home/metal/project/PAutomationCookbook/ch04', 'file1.txt')

#os.path.split()返回目录和文件之间分割路径、文件名的元组。

 

posted @ 2022-04-11 11:50  轻舞飞洋  阅读(22)  评论(0编辑  收藏  举报