listdir 、getcwd、chdir的使用
来源:互联网 发布:java健身房信息系统 编辑:程序博客网 时间:2024/06/01 10:41
``os`` 模块也包含了一些用于目录处理的函数.
``listdir`` 函数返回给定目录中所有文件名(包括目录名)组成的列表, 如
[Example 1-28 #eg-1-28] 所示. 而 Unix 和 Windows 中使用的当前目录和父目录标记(. 和 .. )不包含在此列表中.
====Example 1-28. 使用 os 列出目录下的文件====[eg-1-28]
```
File: os-example-5.py
import os
for file in os.listdir("samples"):
print file
*B*sample.au
sample.jpg
sample.wav
...*b*
```
``getcwd`` 和 ``chdir`` 函数分别用于获得和改变当前工作目录. 如 [Example 1-29 #eg-1-29] 所示.
====Example 1-29. 使用 os 模块改变当前工作目录====[eg-1-29]
```
File: os-example-4.py
import os
# where are we?
cwd = os.getcwd()
print "1", cwd
# go down
os.chdir("samples")
print "2", os.getcwd()
# go back up
os.chdir(os.pardir)
print "3", os.getcwd()
*B*1 /ematter/librarybook
2 /ematter/librarybook/samples
3 /ematter/librarybook*b*
``listdir`` 函数返回给定目录中所有文件名(包括目录名)组成的列表, 如
[Example 1-28 #eg-1-28] 所示. 而 Unix 和 Windows 中使用的当前目录和父目录标记(. 和 .. )不包含在此列表中.
====Example 1-28. 使用 os 列出目录下的文件====[eg-1-28]
```
File: os-example-5.py
import os
for file in os.listdir("samples"):
print file
*B*sample.au
sample.jpg
sample.wav
...*b*
```
``getcwd`` 和 ``chdir`` 函数分别用于获得和改变当前工作目录. 如 [Example 1-29 #eg-1-29] 所示.
====Example 1-29. 使用 os 模块改变当前工作目录====[eg-1-29]
```
File: os-example-4.py
import os
# where are we?
cwd = os.getcwd()
print "1", cwd
# go down
os.chdir("samples")
print "2", os.getcwd()
# go back up
os.chdir(os.pardir)
print "3", os.getcwd()
*B*1 /ematter/librarybook
2 /ematter/librarybook/samples
3 /ematter/librarybook*b*
阅读全文
0 0
- listdir 、getcwd、chdir的使用
- chdir getcwd 函数的使用过程中出现的问题
- chdir & getcwd
- chdir,getcwd
- getcwd()和chdir()的用法 python
- chdir与getcwd
- chdir与getcwd
- chdir与getcwd
- 22-与目录有关的函数-chdir 与 getcwd
- chdir、fchdir 和 getcwd 函数
- chdir、fchdir 和 getcwd 函数
- dup chdir getcwd ls 管道
- chdir,fchdir,and getcwd Functions
- python os.getcwd()和os.listdir
- 目录函数mkdir/rmdir/getcwd/chdir
- Python使用os.listdir()函数来得目录内容的介绍
- Python使用os.listdir()函数来得目录内容的介绍
- Python使用os.listdir()函数来得目录内容的介绍
- J
- Leetcode:350. Intersection of Two Arrays II 求两个数组的交集
- Educational Codeforces Round 32
- Hibernate(三)入门操作
- 如何在 Vue.js 中使用第三方库
- listdir 、getcwd、chdir的使用
- Vue.js插件开发
- hannnnah_j’s Biological Test HDU
- makedirs、removedirs 和 mkdir、rmdir
- 团队天梯赛L1-023. 输出GPLT
- mysql触发器使用---转载
- transient修饰符
- html基础
- 双十一选IT培训?都在这,超低价