4 python os模块用法的一个例子

1 查看当前目录

pwd
u'/home/huxiang/work/test'

2 利用os.listdir列举当前目录所有文件

import os
file_list = [x for x in os.listdir("/home/huxiang/work/test")]
for file_name in file_list:
    print file_name
os.ipynb
2.py
test.py
input_data.py
mnist1.py
mnist.py
others.txt~
Untitled3.ipynb
Untitled1.ipynb
config.py
shenmu.txt~
eesen-master
Untitled.ipynb
shenmu.txt
Untitled Document
Untitled Document~
others.txt
fully_connected_feed.py~
lstm_rnn.py
input_data.pyc
Untitled2.ipynb
keep
.ipynb_checkpoints
yunmu.txt
yunmu.txt~
mnist1.py~
11.3 python with as的用法.ipynb
cifar10
MNIST_data
config.pyc
fully_connected_feed.py
hello.py
ASR
000050275.WAV
tensorboard.py

3 ls *txt

file_list = [x for x in os.listdir(dira) if x.split('.')[-1] == 'txt']
print file_list
['1.txt', '1and2.txt', '22.txt', 'shenmu.txt', 'others.txt', '2.txt', 'yunmu.txt']

你可能感兴趣的:(python)