Ask Your Question
0

Anyone can help me to debug the Python program?

asked 2013-11-19 03:42:33 -0500

Bob gravatar image

updated 2013-12-31 22:24:27 -0500

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
edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
2

answered 2013-11-19 11:25:27 -0500

terrytian gravatar image

updated 2013-11-19 20:54:35 -0500

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]
edit flag offensive delete link more

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 ( 2013-11-19 20:34:45 -0500 )edit
1

answered 2013-11-19 20:38:13 -0500

jconto gravatar image

updated 2013-11-19 20:39:07 -0500

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

case_root = os.path.splitext(CASE)

edit flag offensive delete link more

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 ( 2013-11-19 21:11:25 -0500 )edit

@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 ( 2013-11-19 21:24:27 -0500 )edit

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

jconto gravatar imagejconto ( 2013-11-19 22:18:41 -0500 )edit

Thanks. it works well.

Bob gravatar imageBob ( 2013-11-20 00:53:48 -0500 )edit
0

answered 2013-11-19 14:33:57 -0500

what python version are you using?

edit flag offensive delete link more

Comments

@Saimani Kumar Python 2.7

Bob gravatar imageBob ( 2013-11-19 20:21:18 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

Stats

Asked: 2013-11-19 03:42:33 -0500

Seen: 849 times

Last updated: Dec 31 '13