Python获取目录、文件的注意事项
Python获取指定路径下的子目录和文件有两种方法:
os.listdir(dir)和os.walk(dir),前者列出dir目录下的所有直接子目录和文件的名称(均不包含完整路径),如
>>> os.listdir(r'E:')
['$RECYCLE.BIN', 'Download', 'test.txt', 'data', 'MyDownloads', 'System Volume Information', 'VSPath', 'Youku Files']
>>>
后者,递归地获取dir目录下的所有子目录、文件,采用深度遍历的方式,详见http://www.cnblogs.com/herbert/archive/2013/01/07/2848892.html
更详细的操作目录和文件的方法,如判断是否是文件,参考http://www.cnblogs.com/yuxc/archive/2011/08/01/2124012.html
需要注意的是,使用os.walk()函数内部实际上调用了os.listdir(),另外,dir也是一个Python内建的函数,因此在任何地方都最好不要把dir作为参数变量传入函数(可以换个名字,如_dir等),否则可能会报奇怪的错误。
以下是我遇到的一个错误:
import os
def getAllDataFiles(directory):
dirsOrFiles=os.listdir(dir)
print dirsOrFiles if __name__=='__main__':
print os.listdir('E:')
getAllDataFiles('E:')
第7行可以正确输出,第8行则会报错:TypeError: coercing to Unicode: need string or buffer, builtin_function_or_method found,本来出错的原因应该是不能识别dir变量,但因为python这里把它识别为内建的dir()函数,因此没有报类似"NameError: name 'dir' is not defined"这样的错误。
Python获取目录、文件的注意事项的更多相关文章
- python获取当前文件路径
python获取当前文件路径 学习了:https://www.cnblogs.com/strongYaYa/p/7200357.html https://blog.csdn.net/heatdeath ...
- python 获取当前文件夹路径及父级目录的几种方法
获取当前文件夹路径及父级目录: import os current_dir = os.path.abspath(os.path.dirname(__file__)) print(current_dir ...
- python模块目录文件后续
1,新增PythonModule加载path Ruiy tip(关于python list[]数据库类型特殊你懂的!append(""),extend([""] ...
- Python获取指定文件夹下的文件名
本文采用os.walk()和os.listdir()两种方法,获取指定文件夹下的文件名. 一.os.walk() 模块os中的walk()函数可以遍历文件夹下所有的文件. os.walk(top, t ...
- python安装whl文件的注意事项(windows系统)
首先给大家来一波福利,在没有连接外网(互联网)的情况下,只有公司内网或者断网情况下,需要安装python的一些依赖,不会操作的同学可能就会遇到麻烦.这里教大家离线安装python依赖. 方法:使用.w ...
- 关于Python获取图片文件二进制数据的问题(获取为空)
在搭建fastdfs文件系统的时候遇到了点问题,在测试上传文件数据流的时候,需要Python来获取本地文件的二进制流 from fdfs_client.client import Fdfs_clien ...
- python获取目录下所有文件
#方法1:使用os.listdir import os for filename in os.listdir(r'c:\\windows'): print filename #方法2:使用glob模块 ...
- (26)Python获取某个文件存放的相对路径(更改任意目录下保持不变)
import os import platform def getSeparator(): ''' 获取不同平台下的斜杠符号 :return: Created by Wu Yongcong 2017- ...
- python获取目录下文件夹名称
path = '/opt' dirs = os.listdir(path) for dir in dirs: print dir
随机推荐
- Maven 初学(一)基本概念
Pom中有三个主要元素 Groupid,artifactid,version goupid 是一个组织唯一的标识 例如 com.ibm.www artifactid 是一个工程呢ID ...
- Java基础-gs(垃圾回收)
Java垃圾回收概况 Java GC(Garbage Collection,垃圾收集,垃圾回收)机制,是Java与C++/C的主要区别之一,作为Java开发者,一般不需要专门编写内存回收和垃圾清理代 ...
- 【HDU 5363】Key Set
题 Description soda has a set $S$ with $n$ integers $\{1, 2, \dots, n\}$. A set is called key set if ...
- BZOJ-3225 立方体覆盖 线段树+扫描线+乱搞
看数据范围像是个暴力,而且理论复杂度似乎可行,然后被卡了两个点...然后来了个乱搞的线段树+扫描线.. 3225: [Sdoi2008]立方体覆盖 Time Limit: 2 Sec Memory L ...
- POJ2288 Islands and Bridges
Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...
- ActivityInfo taskAffinity
通常在Manifest里面使用 <application android:icon="@drawable/icon" android:label="@string/ ...
- 代码重构-2 简单不变的 if else 用字典代替
原代码 private string GetExDesc(string lotteryCode) { string exDesc = "抽奖"; if (lotteryCode.T ...
- UVa OJ 140 - Bandwidth (带宽)
Time limit: 3.000 seconds限时3.000秒 Problem问题 Given a graph (V,E) where V is a set of nodes and E is a ...
- Applying Eigenvalues to the Fibonacci Problem
http://scottsievert.github.io/blog/2015/01/31/the-mysterious-eigenvalue/ The Fibonacci problem is a ...
- 使用I/O 系统调用--copy.c
作为Linux/Unix 系统编程入门,小生按照自己可以理解的方式,改写了源copy.c源代码来自:Linux/UNIX 系统编程手册 上册 P57 #include <stdio.h>/ ...