您的位置:首页 > 编程语言 > Python开发

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

2016-11-29 09:31 417 查看

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 os