여기에서 찾은 스크립트의 일부를 사용하여 Windows PC의 디렉토리를 실행하여 XML 파일을 생성합니다. 그러나 위의 오류가 발생하여 오류를 처리하는 방법을 모르겠습니다. 시도 / 예외를 추가했지만 여전히 충돌합니다. "DirTree3 ("C : / ")"를 "DirTree3 ((os.getcwd ())"로 대체하여 디렉토리를 현재 작업 디렉토리로 설정하면 완벽하게 작동합니다.
def DirTree3(path):
try:
result = '<dir>%s\n' % xml_quoteattr(os.path.basename(path))
for item in os.listdir(path):
itempath = os.path.join(path, item)
if os.path.isdir(itempath):
result += '\n'.join(' ' + line for line in
DirTree3(os.path.join(path, item)).split('\n'))
elif os.path.isfile(itempath):
result += ' <file> %s </file>\n' % xml_quoteattr(item)
result += '</dir> \n'
return result
except Exception:
pass
print '<DirectoryListing>\n' + DirTree3("C:/") + '\n</DirectoryListing>'
참고로이 스크립트는 관리자 권한이없는 시스템에서 실행되므로 관리자로 실행하는 것은 옵션이 아닙니다.
경로 액세스 오류를 가져오고 무시하고 싶다는 귀하의 의견을 바탕으로 가능한 한 최선을 다해 아래 답변의 코드를 수정했습니다. 다른 유형의 예외가 발생하는 경우에도 종료됩니다.
def DirTree3(path):
try:
result = '<dir>%s\n' % xml_quoteattr(os.path.basename(path))
try:
items = os.listdir(path)
except WindowsError as exc:
return '<error> {} </error>'.format(xml_quoteattr(str(exc)))
for item in items:
itempath = os.path.join(path, item)
if os.path.isdir(itempath):
result += '\n'.join(' ' + line for line in
DirTree3(os.path.join(path, item)).split('\n'))
elif os.path.isfile(itempath):
result += ' <file> %s </file>\n' % xml_quoteattr(item)
result += '</dir> \n'
return result
except Exception as exc:
print('exception occurred: {}'.format(exc))
raise
print '<DirectoryListing>\n' + DirTree3("C:/") + '\n</DirectoryListing>'
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다