红联Linux门户
Linux帮助

python的问题

发布时间:2009-07-30 17:45:40来源:红联作者:blueskysee
#!/usr/bin/python
import os,sys
def getlist(path,file):
s=os.listdir(path)
#print s
for i in range(0,len(s)):
if file in s:
if file and os.path.isfile(file):
os.chdir(path)
h = open("%s" %file)
infile = h.readlines() or []
h.close()
if infile:
infile = [line.rstrip('\r\n') for line in infile]
return infile
else:
print ("'%s' is a directory "%file)
sys.exit()
else:
print ("file or directory '%s' is not exists" %file)
print ("the path included files or directory follow as:")
print os.listdir(path)
sys.exit()
x=getlist("/root/python/service/oss","list.txt")
print x
为什么我 if file and os.path.isfile(file):总是返回了错误呢?
不管是不是文件都执行了
else:
print ("'%s' is a directory "%file)
sys.exit()
我很纳闷,不知道为什么?那为高手指点下。
文章评论

共有 3 条评论

  1. zshd 于 2011-07-20 13:33:54发表:

    python 代码太难懂了。

  2. congsong1319 于 2010-12-27 23:49:38发表:

    中间的file试试用完整的路径:

  3. chairman 于 2009-08-17 17:21:50发表:

    好东西