2009-06-15 5 views
0

Python 2.6.2でディレクトリをスキャンしてSVGをスキャンし、サイズが大きすぎるとサイズを変更するスクリプトを作成しました。私はこれを私の家庭用マシン(Vista、Python 2.6.2)に書いて問題のないいくつかのフォルダを処理しました。今日、私は仕事用コンピュータ(XP SP2、Python 2.6.2)でこれを試してみました。ファイルがディレクトリに入っていても、すべてのファイルに対してIOErrorsを取得しています。私はすべてを試したと思うし、どこから行くのか分からない。私は初心者ですので、これは単純なものかもしれません。どんな助けもありがとう。IOErrorファイルが存在していても "そのようなファイルやフォルダはありません"

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 
+0

try-exceptを使用しないで、エラーの原因を教えてください。 – SilentGhost

答えて

1

開こうとしているファイルへの完全なパスを取得するには、os.path.join(path, tfile)が不足しているように見えます。現在のところ、現在のディレクトリ内のファイルに対してのみ有効です。

+0

成功!ありがとうございます!あなたは紳士で学者です。 – nosleep

0

おそらくセキュリティ上の問題ですか?フォルダにファイルを作成する権利がない可能性があります。

関連する問題