First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!

Ask Your Question
0

Anyone can help me to debug the Python program?

asked Nov 19 '13

Bob gravatar image

updated Jan 1 '14

JervisW gravatar image

I want to find all the .sav files in E:\007, and conver the generators and loads use cong( ) and conl( ) function, but there are some errors in the program I wrote, anyone can help me ?
Thank you very much!

import os,sys
sys.path.append(r"C:\Program Files\PTI\PSSE33\PSSBIN")
os.environ['PATH'] = r"C:\Program Files\PTI\PSSE33\PSSBIN;" + os.environ['PATH']
os.chdir(r"E:\007")
import psspy
import redirect
import glob
PYTHONPATH = r'C:\Program Files\PTI\PSSE33\EXAMPLE'


sys.path.append(PYTHONPATH)
os.environ['PATH'] += ';' + PYTHONPATH

redirect.psse2py()

for CASE in glob.glob(os.path.join('E:\\007\\','*.sav')):
    psspy.psseinit(150000)
    psspy.case(CASE)

    psspy.cong(0)
    psspy.conl(0,1,1,[0,0],[0.0,0.0,0.0,0.0])
    psspy.conl(0,1,2,[0,0],[0.0,0.0,0.0,0.0])
    psspy.conl(0,1,3,[0,0],[0.0,0.0,0.0,0.0])

    case_root = os.path.splitext(sys.argv[1])[0]
    psspy.save(case_root + "_C.sav")

There ERRORS are as follow:

Traceback (most recent call last):
  File "E:\007\21.py", line 32, in <module>
    case_root = os.path.splitext(sys.argv[1])[0]
IndexError: list index out of range

3 answers

Sort by » oldest newest most voted
2

answered Nov 19 '13

terrytian gravatar image

updated Nov 20 '13

This is because the list sys.argv contains only one element, that is your python script name, 'E:\007\21.py'.

Since python index starts from 0, the list element sys.argv[1] is actually the second element of the list sys.arg.

so you should use the following:

case_root = os.path.splitext(sys.argv[0])[0]

oh, in that case you should use:

case_root = os.path.splitext(CASE)[0]
link

Comments

@terrytian Thanks for your answer, but it's look like no the thing. What I want to do is to find all the .sav files in the file box E:\007 ,and convert all the generators and loads for dynamic simulation, then the converted files named like Origianl file1.sav and the new name is 1_C.sav

Bob gravatar imageBob (Nov 20 '13)
1

answered Nov 20 '13

jconto gravatar image

updated Nov 20 '13

to have a converted case name derive from the source sav file, use the following:

case_root = os.path.splitext(CASE)

link

Comments

@jconto Thanks Traceback (most recent call last): File "E:\007\gensp.py", line 42, in <module> psspy.save(case_root + "_C.sav") TypeError: can only concatenate tuple (not "str") to tuple In other woeds case_root = os.path.splitext(case) type(case_root) is tuple not str

Bob gravatar imageBob (Nov 20 '13)

@jconto The only thing I want to do is to convert the name of files like savnw.sav bench.sav to savnw_C.sav bench_C.sav . In other words we want to get the name of savnw.sav by the format of string to use the python command sfile = "savnw"+"_C.sav"

Bob gravatar imageBob (Nov 20 '13)

splitext returns two values. 'ext' is the extension of the CASE filename, use: case_root, ext = os.path.splitext(CASE)

jconto gravatar imagejconto (Nov 20 '13)

Thanks. it works well.

Bob gravatar imageBob (Nov 20 '13)
0

answered Nov 19 '13

what python version are you using?

link

Comments

@Saimani Kumar Python 2.7

Bob gravatar imageBob (Nov 20 '13)

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.
Want to format code in your answer? Here is a one minute demo on Youtube

Add Answer

[hide preview]

Question Tools

Stats

Asked: Nov 19 '13

Seen: 862 times

Last updated: Dec 31 '13