1

I want to rename a file called decon.out using two variables in my program. So far I have

gwf = input ("Enter value: ")
myList = os.listdir('.')
    for myFile in myList:
        if re.match("^HHEMQZ", myFile):
            numE = myFile
        elif re.match("^HHNMQZ", myFile):
            numN = myFile
        else: 
            den = myFile
os.rename('decon.out', 'RF'+gwf+''+numE+'')

For example, gwf = 2.5 and numE = HHEMQZ20010101

I would then want decon.out to be renamed as RF2.5HHEMQZ20010101 where RF will always be the same. Currently when I run the script I get an error:

Traceback (most recent call last):
  File "RunDeconv.py", line 77, in <module>
    os.rename('decon.out', 'RF'+gwf+''+numE+'')
TypeError: cannot concatenate 'str' and 'float' objects

Any suggestions?

1
  • Are you on Python 2.x? Commented Aug 2, 2013 at 11:05

2 Answers 2

2

Use raw_input() instead, input() interprets the input values as Python code turning your 2.5 input into a float number.

Sign up to request clarification or add additional context in comments.

Comments

0

About the error: in the string concatenation

'RF'+gwf+''+numE+''

all the members must be strings.

You can use

type(gwf)
type(numE)

to check which is a number.

You then just need to

str(gwf)

or

str(numE)

depending on which may be the case. Or probably both gwf and numE need the str() treatment, so your last line of code should look like this:

os.rename('decon.out', 'RF'+str(gwf)+''+str(numE)+'')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.