歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> 關於Unix >> python的問題

python的問題

日期:2017/2/28 11:07:01   编辑:關於Unix


#!/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()
我很納悶,不知道為什麼?那為高手指點下。
Copyright © Linux教程網 All Rights Reserved