python os.path.isfile()因参数问题判断错误的解决
目的:找出路径坐在的所有python文件(.py结尾),返回列表。
代码:
deflist_py(path=None): ifpath==None: path=os.getcwd() return[fnameforfnameinos.listdir(path) ifos.path.isfile(fname)andfname.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)
作为参数
以上这篇pythonos.path.isfile()因参数问题判断错误的解决就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。