Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 248003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:18:23+00:00 2026-05-11T21:18:23+00:00

I wrote a script in Python 2.6.2 that scans a directory for SVG’s and

  • 0

I wrote a script in Python 2.6.2 that scans a directory for SVG’s and resizes them if they are too large. I wrote this on my home machine (Vista, Python 2.6.2) and processed a few folders with no problems. Today, I tried this on my work computer (XP SP2, Python 2.6.2) and I get IOErrors for every file, even though files are in the directory. I think I’ve tried everything, and am unsure where to go from here. I am a beginner so this may be something simple. Any help would be appreciated.

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:18:24+00:00Added an answer on May 11, 2026 at 9:18 pm

    It looks to me like you are missing a os.path.join(path, tfile) to get the full path to the file you want to open. Currently it should only work for files in the current directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As you've said it can be an MSI, I'd suggest… May 12, 2026 at 12:49 am
  • Editorial Team
    Editorial Team added an answer It's easy enough to write your own comparison function: function… May 12, 2026 at 12:49 am
  • Editorial Team
    Editorial Team added an answer $('img.removeme').closest('li').remove(); Should do it. The problem with parents() is that… May 12, 2026 at 12:49 am

Related Questions

I wrote a script in Python 2.6.2 that scans a directory for SVG's and
If I have a Python script that requires at least a particular version of
I'm trying to write (what I thought would be) a simple bash script that
I am working on porting over a database from a custom MSSQL CMS to
Referring on this question , I have a similar -but not the same- problem..

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.