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

本文介绍了使用Python进行目录操作的方法,包括查看当前工作目录、列出指定目录下的所有文件等基本功能。通过示例展示了如何利用os模块实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值