에 내가 사용하여 서버를 시작합니다. 그러나 알파벳순으로 나열합니다.만들기 파이썬 SimpleHTTPServer 목록 하위 디렉토리는 최고
Windows 셸 스타일로 파일 앞에 나열된 하위 디렉토리로 만들 수 있지만 두 그룹을 알파벳 순으로 정렬 할 수 있습니까?
에 내가 사용하여 서버를 시작합니다. 그러나 알파벳순으로 나열합니다.만들기 파이썬 SimpleHTTPServer 목록 하위 디렉토리는 최고
Windows 셸 스타일로 파일 앞에 나열된 하위 디렉토리로 만들 수 있지만 두 그룹을 알파벳 순으로 정렬 할 수 있습니까?
SimpleHTTPServer.py 파일을 변경해야합니다. 아래의 기능을 약간 변경했습니다. 당신은 대체하고 확인할 수 있습니다
def list_directory(self, path):
"""Helper to produce a directory listing (absent index.html).
Return value is either a file object, or None (indicating an
error). In either case, the headers are sent, making the
interface the same as for send_head().
"""
lst1=[]
lst2=[]
try:
list = os.listdir(path)
for i in list:
if os.path.isdir(os.path.join(path,i)):
lst1.append(i)
elif os.path.isfile(os.path.join(path,i)):
lst2.append(i)
except os.error:
self.send_error(404, "No permission to list directory")
return None
lst1.sort(key=lambda a: a.lower())
#print lst1
#print list
lst2.sort(key=lambda a: a.lower())
f = StringIO()
displaypath = cgi.escape(urllib.unquote(self.path))
f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath)
f.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath)
f.write("<hr>\n<ul>\n")
for name in lst1:
fullname = os.path.join(path, name)
displayname = linkname = name
# Append/for directories or @ for symbolic links
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + "/"
f.write('<li><a href="%s">%s</a>\n'
% (urllib.quote(linkname), cgi.escape(displayname)))
for name in lst2:
fullname = os.path.join(path, name)
displayname = linkname = name
# Append/for directories or @ for symbolic links
if os.path.islink(fullname):
displayname = name + "@"
# Note: a link to a directory displays with @ and links with/
f.write('<li><a href="%s">%s</a>\n'
% (urllib.quote(linkname), cgi.escape(displayname)))
f.write("</ul>\n<hr>\n</body>\n</html>\n")
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(length))
self.end_headers()
return f
완벽! 감사합니다 Sandeep. 나는 당신의 대답을 약간 수정하여 2 개의 목록을 '목록'으로 연결하고 그 아래에있는 원래의 코드로 계속 간다. –
또한 하위 디렉토리에 들어갔을 때 상위 디렉토리를 가리 키도록 목록 맨 위에 링크 (예 : "..")를 추가하는 방법을 보여줄 수 있습니까? –
잘 알고 있습니다 :-) 나는'..'을 표시 할 것인지 확인하고 알려 줄 것입니다. –
그것이 작동하는지 여부를 알려주십시오 –