文件夹中有许多子目录,使用tree命令列出到二级的目录
sudo apt install tree
tree -L 2 > tree.txt
os.listdir( ) --返回当前目录下所有文件列表
import os
filenames = os.listdir('.')
for filename in filenames:
print(filename)
print(os.path.join('.',filename))
列表_列表解析_切片_元组.ipynb
./列表_列表解析_切片_元组.ipynb
变量和字符串.md
./变量和字符串.md
if 语句.md
./if 语句.md
sitka_weather_07-2014.csv
./sitka_weather_07-2014.csv
.ipynb_checkpoints
./.ipynb_checkpoints
if 语句.ipynb
./if 语句.ipynb
pcc-master.zip
./pcc-master.zip
Untitled.ipynb
./Untitled.ipynb
变量和字符串.ipynb
./变量和字符串.ipynb
列表_列表解析_切片_元组.md
./列表_列表解析_切片_元组.md
字典.ipynb
./字典.ipynb
csv.ipynb
./csv.ipynb
pcc-master
./pcc-master
os.walk( ) 用于通过在目录树中游走输出在目录中的文件名,向上或者向下
os.walk(rootdir)函数返回一个三元素元祖(root,dirs,files)
root 所指的是当前正在遍历的这个文件夹的本身的地址
dirs 是一个 list ,内容是该文件夹中所有的目录的名字(不包括子目录)
files 同样是 list , 内容是该文件夹中所有的文件(不包括子目录)
import os
for root, dirs, files in os.walk("."):
for dir_name in dirs:
print(dir_name) #返回当前目录下的所有目录名
.ipynb_checkpoints
pcc-master
chapter_10
chapter_20
chapter_09
chapter_07
chapter_16
chapter_01
chapter_02
chapter_18
chapter_19
chapter_03
chapter_05
users
learning_logs
learning_log_django_2.0
learning_log_django_2.1
figures
learning_log
migrations
templates
......
......
templates
users
migrations
templates
learning_logs
import os
for root, dirs, files in os.walk("."):
for file_name in files:
print(file_name) #返回当前目录下的所有文件名
列表_列表解析_切片_元组.ipynb
变量和字符串.md
if 语句.md
sitka_weather_07-2014.csv
if 语句.ipynb
pcc-master.zip
变量和字符串.ipynb
python遍历文件夹下所有文件.ipynb
列表_列表解析_切片_元组.md
字典.ipynb
csv.ipynb
列表_列表解析_切片_元组-checkpoint.ipynb
字典-checkpoint.ipynb
变量和字符串-checkpoint.ipynb
python遍历文件夹下所有文件-checkpoint.ipynb
if 语句-checkpoint.ipynb
csv-checkpoint.ipynb
.gitignore
UPDATES.md
README.md
remember_me.py
pi_30_digits.txt
alice.txt
alice.py
little_women.txt
pi_million_digits.txt
greet_user.py
.....
......
settings.py
wsgi.py
urls.py
__init__.py
bicycles.py
motorcycles.py
cars.py
voting.py
cars.py
banned_users.py
toppings.py
amusement_park.py
magic_number.py
import os
for root, dirs, files in os.walk("."):
for dir_name in dirs:
print(os.path.join(root,dir_name)) #返回当前目录下所有目录的相对路径
./.ipynb_checkpoints
./pcc-master
./pcc-master/chapter_10
./pcc-master/chapter_20
./pcc-master/chapter_09
./pcc-master/chapter_07
./pcc-master/chapter_16
./pcc-master/chapter_01
./pcc-master/chapter_02
./pcc-master/chapter_04
./pcc-master/chapter_06
./pcc-master/chapter_08
./pcc-master/appendix_a
./pcc-master/appendix_b
./pcc-master/chapter_17
./pcc-master/appendix_d
.......
.......
./pcc-master/chapter_19/users
./pcc-master/chapter_19/learning_logs
./pcc-master/chapter_19/figures
./pcc-master/chapter_19/learning_log
./pcc-master/chapter_19/users/migrations
./pcc-master/chapter_19/users/templates
./pcc-master/chapter_19/users/templates/users
./pcc-master/chapter_19/learning_logs/migrations
./pcc-master/chapter_19/learning_logs/templates
./pcc-master/chapter_19/learning_logs/templates/learning_logs
import os
for root, dirs, files in os.walk("."):
for file_name in files:
print(os.path.join(root,file_name)) #返回当前目录下所有文件的相对路径
./列表_列表解析_切片_元组.ipynb
./变量和字符串.md
./if 语句.md
./sitka_weather_07-2014.csv
./if 语句.ipynb
./pcc-master.zip
./变量和字符串.ipynb
./python遍历文件夹下所有文件.ipynb
./列表_列表解析_切片_元组.md
./字典.ipynb
./csv.ipynb
./.ipynb_checkpoints/列表_列表解析_切片_元组-checkpoint.ipynb
./.ipynb_checkpoints/字典-checkpoint.ipynb
./.ipynb_checkpoints/变量和字符串-checkpoint.ipynb
./.ipynb_checkpoints/python遍历文件夹下所有文件-checkpoint.ipynb
./.ipynb_checkpoints/if 语句-checkpoint.ipynb
./.ipynb_checkpoints/csv-checkpoint.ipynb
./pcc-master/.gitignore
./pcc-master/UPDATES.md
./pcc-master/README.md
./pcc-master/chapter_10/remember_me.py
./pcc-master/chapter_10/pi_30_digits.txt
....
....
./pcc-master/chapter_05/voting.py
./pcc-master/chapter_05/cars.py
./pcc-master/chapter_05/banned_users.py
./pcc-master/chapter_05/toppings.py
./pcc-master/chapter_05/amusement_park.py
./pcc-master/chapter_05/magic_number.py