python os.path.isfile()因参数问题判断错误
2018-02-10 16:00
645 查看
目的: 找出路径坐在的所有python文件(.py结尾),返回列表。
代码:
错误:
不传入参数没有问题
但是传入参数后
返回:
一度以为将路径作为参数会出问题,但是在删除列表中if条件后又正常返回。所以显而易见。
解决:
接收的参数是路径+文件名,所以不单单是
而应该用:
作为参数
———关注我的公众号,一起学数据挖掘————
代码:
def list_py(path = None): if path == None: path = os.getcwd() return [fname for fname in os.listdir(path) if os.path.isfile(fname) and fname.endswith('.py')
错误:
不传入参数没有问题
>>> list_py() ['cmdtest.py', 'datatest.py', 'excel_combine.py', 'input.py', 'iotest.py', 'load_data_test.py', 'numpy_arraytest.py', 'ooptest.py', 'pandas_test.py', 'str_test.py', 'test.py', 'wordstats.py']
但是传入参数后
>>> list_py('D:\Python\PythonProject\data_mining_with_python') []
返回:
[]
一度以为将路径作为参数会出问题,但是在删除列表中if条件后又正常返回。所以显而易见。
解决:
os.path.isfile()
接收的参数是路径+文件名,所以不单单是
fname或者
path。
而应该用:
os.path.join(path,fname)
作为参数
———关注我的公众号,一起学数据挖掘————
相关文章推荐
- os.path.isfile() 判断问题
- python中由于中文路径引起的os.path.isfile(imgpath) == False问题
- os.path.isfile的错误
- python的os.path.join()参数是绝对路径的问题
- os.path.isfile 使用误区
- Python_API_File and Directory Access_os.path.expanduser
- python中os.path.dirname(__file__)的使用
- Python有关 os.path.dirname(__file__)
- Python os.path.dirname(__file__) os.path.join(str,str)
- python中os.path.dirname(__file__)的使用
- spring错误处理 Build path is incomplete. Cannot find class file for org.springframework.aop.Advisor
- python中的os.path.dirname(__file__)的使用
- python中的os.path.dirname(__file__)的使用
- Python简单CGI服务器os.execve(scriptfile,args,os.environ)权限错误
- python中的os.path.dirname(__file__)的使用
- Eclipse错误提示:The project was not built since its build path is incomplete. Cannot find the class file
- 转: Python中的os.path.dirname(__file__)
- Python_API_File and Directory Access_os.path.splitext
- 使用GreenDao 报"This check is to prevent accidental file generation into a wrong path."错误
- hadoop 问题 copyFromLocal: java.io.FileNotFoundException: Parent path is not a directory: /user/admini