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

Ask Your Question
1

How to short the string variable ?

asked Nov 1 '12

Mongo gravatar image

updated Nov 1 '12

JervisW gravatar image

Hi folks,

When I use this:

filename = os.path.basename(savecases)

I get something like "summer_light.sav"; the extension comes as well. Please, is there a python code to shorten it to "summer_light" only, I mean, to get rid of the extension ?

Thanks a lot, Andre

2 answers

Sort by » oldest newest most voted
2

answered Nov 1 '12

cajief gravatar image

There are a number of ways to do this in Python. One approach is to use "splitext" instead of "basename":

filename, extension = os.path.splitext(savecases)

or if you're only interested in the file name and not the extension...

filename = os.path.splitext(savecases)[0]
link

Comments

Thanks a lot Cajief, much much appreciated !

Mongo gravatar imageMongo (Nov 3 '12)
0

answered Nov 3 '12

Mongo gravatar image

Thanks Cajief, much much appreciated !!!

link

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 1 '12

Seen: 342 times

Last updated: Nov 03 '12