Python 2.6.2에서 SVG 디렉토리를 검색하고 크기가 너무 큰 경우 크기를 조정하는 스크립트를 작성했습니다. 나는 이것을 가정용 컴퓨터 (Vista, Python 2.6.2)에 작성하고 몇 가지 폴더를 문제없이 처리했습니다. 오늘 필자는 필자의 작업 컴퓨터 (XP SP2, Python 2.6.2)에서 이것을 시도했지만 파일이 디렉토리에 있더라도 모든 파일에 대해 IOErrors를 얻었습니다. 나는 모든 것을 시험해 보았고 여기에서 어디로 가야할지 확신 할 수 없다. 나는 초보자이기 때문에 이것은 단순 할 수 있습니다. 어떤 도움을 주시면 감사하겠습니다.파일이 존재하더라도 IOError "no such file or folders"가 있습니다.
import xml.etree.ElementTree as ET
import os
import tkFileDialog
#--------------------------------------
#~~~variables
#--------------------------------------
max_height = 500
max_width = 428
extList = ["svg"]
proc_count = 0
resize_count = 0
#--------------------------------------
#~~~functions
#--------------------------------------
def landscape_or_portrait():
resize_count +=1
if float(main_width_old)/float(main_height_old) >= 1.0:
print "picture is landscape"
resize_width()
else:
print "picture is not landscape"
resize_height()
return
def resize_height():
print "picture too tall"
#calculate viewBox and height
viewBox_height_new = max_height
scaleFactor = (float(main_height_old) - max_height)/max_height
viewBox_width_new = float(main_width_old) * scaleFactor
#calculate main width and height
main_height_new = str(viewBox_height_new) + "px"
main_width_new = str(viewBox_width_new) + "px"
viewBox = "0 0 " + str(viewBox_width_new) + " " + str(viewBox_height_new)
inputFile = file(tfile, 'r')
data = inputFile.read()
inputFile.close()
data = data.replace(str(tmain_height_old), str(main_height_new))
data = data.replace(str(tmain_width_old), str(main_width_new))
#data = data.replace(str(tviewBox), str(viewBox))
outputFile = file(tfile, 'w')
outputFile.write(data)
outputFile.close()
return
def resize_width():
print "picture too wide"
#calculate viewBox width and height
viewBox_width_new = max_width
scaleFactor = (float(main_width_old) - max_width)/max_width
viewBox_height_new = float(main_height_old) * scaleFactor
#calculate main width and height
main_height_new = str(viewBox_height_new) + "px"
main_width_new = str(viewBox_width_new) + "px"
viewBox = "0 0 " + str(viewBox_width_new) + " " + str(viewBox_height_new)
inputFile = file(tfile, 'r')
data = inputFile.read()
inputFile.close()
data = data.replace(str(tmain_height_old), str(main_height_new))
data = data.replace(str(tmain_width_old), str(main_width_new))
#data = data.replace(str(tviewBox), str(viewBox))
outputFile = file(tfile, 'w')
outputFile.write(data)
outputFile.close()
return
#--------------------------------------
#~~~operations
#--------------------------------------
path = tkFileDialog.askdirectory()
for tfile in os.listdir(path):
#print tfile
t2file = tfile
if tfile.find(".") >= 0:
try :
if t2file.split(".")[1] in extList:
print "now processing " + tfile
tree = ET.parse(tfile)
proc_count+=1
# Get the values of the root(svg) attributes
root = tree.getroot()
tmain_height_old = root.get("height")
tmain_width_old = root.get("width")
tviewBox = root.get("viewBox")
#clean up variables, remove px for float conversion
main_height_old = tmain_height_old.replace("px", "", 1)
main_width_old = tmain_width_old.replace("px", "", 1)
#check if they are too large
if float(main_height_old) > max_height or float(main_width_old) > max_width:
landscape_or_portrait()
except Exception,e:
print e
try-except를 사용하지 않고 오류의 원인을 알려 주면 더 유용 할 것입니다. – SilentGhost